繁体   English   中英

用下划线过滤JSON

[英]Filtering JSON with underscore

给定这样的数据结构:

"items":
         {
         "Groups":[
            {
               "title":"group 1",
               "SubGroups":[
                  {
                     "title":"sub1",
                     "id" : "1",
                     "items":[
                        {
                           "title":"Ajax request 1",
                        },
                        {
                           "title":"Ajax request 2",
                        }
                     ]
                  },
                    {
                     "title":"sub2",
                     "id" : "2",
                     "items":[
                        {
                           "title":"Ajax request 3",
                        },
                        {
                           "title":"Ajax request 4",
                        }
                     ]
                  }
               ]
            }
         ]

如何根据ID提取子组的所有项目? Ive treid使用find像这样:

var res1 = _.where(listing.items,{id:"2"});

但是得到一个空数组

谢谢

尝试定位子组数组,然后在其中搜索所需的ID。 然后应该返回该子组的属性。

var obj = {
    "Groups": [{
        "title": "group 1",
        "SubGroups": [{
            "title": "sub1",
            "id": "1",
            "items": [{
                "title": "Ajax request 1",
            }, {
                "title": "Ajax request 2",
            }]
        }, {
            "title": "sub2",
            "id": "2",
            "items": [{
                "title": "Ajax request 3",
            }, {
                "title": "Ajax request 4",
            }]
        }]
    }]
}

然后找到这样的价值

_.where(obj.Groups[0].SubGroups, {
   'id': '2'
});

刚刚测试,这似乎有效

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM