簡體   English   中英

嵌套在數組JSON中的數組-如何訪問屬性

[英]Arrays nested in array JSON - how to access properties

我已經提供了一些JSON的支持。 格式如下:

[
[
    {
        "items": [
            {
                "id": "xxxxxxx",
                "name": "xxxxxxxxxx",
                "description": "xxxxxxxxxxx"
            },
            {
                "id": "xxxxxxx",
                "name": "xxxxxxxxxx",
                "description": "xxxxxxxxxxx"
            }
        ],
        "parentId": "xxxxxxxx",
        "title": "xxxxxxxxx",
        "type": "xxxxxxx"
    }
],
[
    {
        "items": [
            {
                "id": "xxxxxxx",
                "name": "xxxxxxxxxx",
                "description": "xxxxxxxxxxx"
            },
            {
                "id": "xxxxxxx",
                "name": "xxxxxxxxxx",
                "description": "xxxxxxxxxxx"
            }
        ],
        "parentId": "xxxxxxxx",
        "title": "xxxxxxxxx",
        "type": "xxxxxxx"
    }
]
]

因此,我有一個名為“數據”的對象。 如果我將“數據”字符串化,這就是上面的內容。 基本上,我有一個parentId,我需要在此JSON中查找該parentId。 我不習慣這種結構,我一直在努力尋找(相對)簡單的解決方案。 我習慣於在頂層使用“項目”之類的東西,並且可以深入研究。

for(var i=0;i<data.length;i++)
{if(data[i][0].parentId=='yourParent_id')
//stuff
}

如果您願意使用庫,則可以在Underscore中執行此操作,

這個:

_(data).findWhere({parentId: idToLookUp});

返回其parentId等於idToLookUp的數組中的對象

小提琴

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM