0

私が提供したいくつかの 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 を検索する必要があります。私はこの構造に慣れておらず、(比較的) 単純な解決策を見つけようとしてもがき苦しんでいます。トップレベルに「アイテム」のようなものがあることに慣れていて、それを掘り下げることができます。

4

2 に答える 2

0
for(var i=0;i<data.length;i++)
{if(data[i][0].parentId=='yourParent_id')
//stuff
}
于 2013-06-22T04:58:01.597 に答える
0

ライブラリの使用を受け入れる場合は、 Underscoreで これを行うことができます。

これ:

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

parentIdが等しい配列内のオブジェクトを返しますidToLookUp

フィドル

于 2013-06-22T05:10:01.310 に答える