0

Struggling with some JSON I've been provided. 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. 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.

DSel
  • 9
  • 4

2 Answers2

0
for(var i=0;i<data.length;i++)
{if(data[i][0].parentId=='yourParent_id')
//stuff
}
bugwheels94
  • 30,681
  • 3
  • 39
  • 60
0

If you're open to using a library, you can do this in Underscore,

This:

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

returns the object in an array where the parentId is equal to idToLookUp

Fiddle

CheapSteaks
  • 4,821
  • 3
  • 31
  • 48