繁体   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