使用lodash方法展平并在json对象中查找值并查找

问题描述:

使用lodash我想找到一个ID为3229的团队.我尝试了以下操作,但未返回任何内容.

Using lodash i want to find a team whose id is 3229. I tried following but it is not returning anything.

    var team = _.chain(data.teams)
        .flatten("divisionTeams")
        .find({"id":3229})
        .value();

这是我的代码.

http://plnkr.co/edit/UDwzRkX3zkYjyf8UwO7I

有关Json数据,请参阅Plunker中的data.js文件.

请注意,由于我正在调用测试api,因此无法更改json数据.

Please note i cannot change the json data since i am calling a test api.

flatten不采用该参数,请参见文档.您需要mappluck divisionTeams.

flatten doesn't take that argument, see docs. You need to either map or pluck the divisionTeams.

_.chain(data.teams)
.pluck('divisionTeams')
.flatten()
.find({id: 3232})
.value();