使用lodash方法展平并在json对象中查找值并查找 [英] Find a value in json object using lodash methods flatten and find

查看:332
本文介绍了使用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();

这篇关于使用lodash方法展平并在json对象中查找值并查找的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆