簡體   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