[英]Arrays nested in array JSON - how to access properties
Struggling with some JSON I've been provided. 我已经提供了一些JSON的支持。 It's in this format:
格式如下:
[
[
{
"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"
}
]
]
So, I have an object named 'data'. 因此,我有一个名为“数据”的对象。 If I stringify 'data' this is what I have above.
如果我将“数据”字符串化,这就是上面的内容。 Basically I have a parentId and I need to lookup that parentId in this JSON.
基本上,我有一个parentId,我需要在此JSON中查找该parentId。 I'm not used to this structure and I'm floundering trying to find a (relatively) simple solution.
我不习惯这种结构,我一直在努力寻找(相对)简单的解决方案。 I'm used to having something like 'items' on the top level and I can drill down through that.
我习惯于在顶层使用“项目”之类的东西,并且可以深入研究。
for(var i=0;i<data.length;i++)
{if(data[i][0].parentId=='yourParent_id')
//stuff
}
If you're open to using a library, you can do this in Underscore , 如果您愿意使用库,则可以在Underscore中执行此操作,
This: 这个:
_(data).findWhere({parentId: idToLookUp});
returns the object in an array where the parentId
is equal to idToLookUp
返回其
parentId
等于idToLookUp
的数组中的对象
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.