How to use underscore.js to produce a flatten result

后端 未结 4 1453
抹茶落季
抹茶落季 2020-12-30 23:53

The json object is

var data = [{\"Parent\":1,\"Child\":[4,5,6]},{\"Parent\":2},{\"Parent\":3}]

How can I use underscore.js chain/map/pluck

4条回答
  •  北海茫月
    2020-12-31 00:16

    Assuming you want to first get the parents and then get the children:

    _.chain(data).pluck("Parent")
                 .concat(_.flatten(_(data).pluck("Child")))
                 .reject(_.isUndefined)
                 .value()
    

提交回复
热议问题