1
{
   "id":0,
   "item":[
      {
         "id":"0-",
         "text":"BlueWing",
         "userdata":[
            {
               "name":"cid",
               "content":"10377"
            }
         ],
         "item":[
            {
               "id":"0-0",
               "text":"Auto",
               "userdata":[
                  {
                     "name":"cid",
                     "content":"10377,10376,10378,10387,10379,10384,10386,10380,10383,10385,10381,10388,10533"
                  }
               ],
               "item":[
                  {
                     "id":"0-0-0",
                     "text":"Auto",
                     "userdata":[
                        {
                           "name":"cid",
                           "content":"10377,10376,10378,10387,10379,10384,10386,10380,10383,10385,10381,10388"
                        }
                     ],
                     "item":[
                        {
                           "id":"0-0-0-0",
                           "text":"testaccount",
                           "userdata":[
                              {
                                 "name":"cid",
                                 "content":"10377,10376,10378,10387,10379,10384,10386,10380,10383,10385,10381,10388"
                              }
                           ],
                           "item":[
                              {
                                 "id":"0-0-0-0-0",
                                 "text":"Add New List",
                                 "userdata":[
                                    {
                                       "name":"clid",
                                       "content":99999
                                    },
                                    {
                                       "name":"cid",
                                       "content":"10377,10376,10378,10387,10379,10384,10386,10380,10383,10385,10381,10388"
                                    }
                                 ]
                              },
                              {
                                 "id":"0-0-0-0-1",
                                 "text":"testaccount",
                                 "userdata":[
                                    {
                                       "name":"cid",
                                       "content":"10377,10376,10378,10387,10379,10384,10386,10380,10383,10385,10381,10388"
                                    }
                                 ],
                                 "item":[
                                    {
                                       "id":"0-0-0-0-1-0",
                                       "text":"Add New List",
                                       "userdata":[
                                          {
                                             "name":"clid",
                                             "content":99999
                                          },
                                          {
                                             "name":"cid",
                                             "content":"10377,10376,10378,10387,10379,10384,10386,10380,10383,10385,10381,10388"
                                          }
                                       ]
                                    },
                                    {
                                       "id":"0-0-0-0-1-1",
                                       "text":"testaccount - 10377,10376,10378,10387,10379,10384,10386,10380,10383,10385,10381,10388",
                                       "userdata":[
                                          {
                                             "name":"cid",
                                             "content":"10377,10376,10378,10387,10379,10384,10386,10380,10383,10385,10381,10388"
                                          }
                                       ],
                                       "item":[
                                          {
                                             "id":"0-0-0-0-1-1-0",
                                             "text":"Add New List",
                                             "userdata":[
                                                {
                                                   "name":"clid",
                                                   "content":99999
                                                },
                                                {
                                                   "name":"cid",
                                                   "content":"10377,10376,10378,10387,10379,10384,10386,10380,10383,10385,10381,10388"
                                                }
                                             ]
                                          }
                                       ]
                                    }
                                 ]
                              }
                           ]
                        }
                     ]
                  },
                  {
                     "id":"0-0-1",
                     "text":"Balise Toyota",
                     "userdata":[
                        {
                           "name":"cid",
                           "content":"10533"
                        }
                     ],
                     "item":[
                        {
                           "id":"0-0-1-0",
                           "text":"Balise Toyota",
                           "userdata":[
                              {
                                 "name":"cid",
                                 "content":"10533"
                              }
                           ],
                           "item":[
                              {
                                 "id":"0-0-1-0-0",
                                 "text":"Add New List",
                                 "userdata":[
                                    {
                                       "name":"clid",
                                       "content":99999
                                    },
                                    {
                                       "name":"cid",
                                       "content":"10533"
                                    }
                                 ]
                              },
                              {
                                 "id":"0-0-1-0-1",
                                 "text":"Balise Toyota",
                                 "userdata":[
                                    {
                                       "name":"cid",
                                       "content":"10533"
                                    }
                                 ],
                                 "item":[
                                    {
                                       "id":"0-0-1-0-1-0",
                                       "text":"Add New List",
                                       "userdata":[
                                          {
                                             "name":"clid",
                                             "content":99999
                                          },
                                          {
                                             "name":"cid",
                                             "content":"10533"
                                          }
                                       ]
                                    },
                                    {
                                       "id":"0-0-1-0-1-1",
                                       "text":"Balise Toyota - 10533",
                                       "userdata":[
                                          {
                                             "name":"cid",
                                             "content":"10533"
                                          }
                                       ],
                                       "item":[
                                          {
                                             "id":"0-0-1-0-1-1-0",
                                             "text":"Add New List",
                                             "userdata":[
                                                {
                                                   "name":"clid",
                                                   "content":99999
                                                },
                                                {
                                                   "name":"cid",
                                                   "content":"10533"
                                                }
                                             ]
                                          },
                                          {
                                             "id":"0-0-1-0-1-1-1",
                                             "text":"Balise.List - 11949",
                                             "userdata":[
                                                {
                                                   "name":"clid",
                                                   "content":11949
                                                },
                                                {
                                                   "name":"cid",
                                                   "content":"10533"
                                                }
                                             ]
                                          }
                                       ]
                                    }
                                 ]
                              }
                           ]
                        }
                     ]
                  }
               ]
            },
            {
               "id":"0-1",
               "text":"BlueWing",
               "userdata":[
                  {
                     "name":"cid",
                     "content":"10667,10668"
                  }
               ],
               "item":[
                  {
                     "id":"0-1-0",
                     "text":"HVAC",
                     "userdata":[
                        {
                           "name":"cid",
                           "content":"10667,10668,10348,10294,10297,10313,10521,10351,10309,10296,10316,10315,10312,10301,10347,10300,10306,10308,10307,10298,10305,10371,10291,10349,10311,10354,10345,10310,10292,10372,10295,10304,10303,10299,10314,10290,10490,10293,10302"
                        }
                     ],
                     "item":[
                        {
                           "id":"0-1-0-0",
                           "text":"HVAC",
                           "userdata":[
                              {
                                 "name":"cid",
                                 "content":"10667,10668"
                              }
                           ],
                           "item":[
                              {
                                 "id":"0-1-0-0-0",
                                 "text":"Add New List",
                                 "userdata":[
                                    {
                                       "name":"clid",
                                       "content":99999
                                    },
                                    {
                                       "name":"cid",
                                       "content":"10667,10668"
                                    }
                                 ]
                              },
                              {
                                 "id":"0-1-0-0-1",
                                 "text":"Mann Plumbing & Heating",
                                 "userdata":[
                                    {
                                       "name":"cid",
                                       "content":"10667"
                                    }
                                 ],
                                 "item":[
                                    {
                                       "id":"0-1-0-0-1-0",
                                       "text":"Add New List",
                                       "userdata":[
                                          {
                                             "name":"clid",
                                             "content":99999
                                          },
                                          {
                                             "name":"cid",
                                             "content":"10667"
                                          }
                                       ]
                                    },
                                    {
                                       "id":"0-1-0-0-1-1",
                                       "text":"Mann Plumbing & Heating - 10667",
                                       "userdata":[
                                          {
                                             "name":"cid",
                                             "content":"10667"
                                          }
                                       ],
                                       "item":[
                                          {
                                             "id":"0-1-0-0-1-1-0",
                                             "text":"Add New List",
                                             "userdata":[
                                                {
                                                   "name":"clid",
                                                   "content":99999
                                                },
                                                {
                                                   "name":"cid",
                                                   "content":"10667"
                                                }
                                             ]
                                          },
                                          {
                                             "id":"0-1-0-0-1-1-1",
                                             "text":"Radius List - 12960",
                                             "userdata":[
                                                {
                                                   "name":"clid",
                                                   "content":12960
                                                },
                                                {
                                                   "name":"cid",
                                                   "content":"10667"
                                                }
                                             ]
                                          }
                                       ]
                                    }
                                 ]
                              },
                              {
                                 "id":"0-1-0-0-2",
                                 "text":"RES Air Conditioning",
                                 "userdata":[
                                    {
                                       "name":"cid",
                                       "content":"10668"
                                    }
                                 ],
                                 "item":[
                                    {
                                       "id":"0-1-0-0-2-0",
                                       "text":"Add New List",
                                       "userdata":[
                                          {
                                             "name":"clid",
                                             "content":99999
                                          },
                                          {
                                             "name":"cid",
                                             "content":"10668"
                                          }
                                       ]
                                    },
                                    {
                                       "id":"0-1-0-0-2-1",
                                       "text":"RES Air Conditioning - 10668",
                                       "userdata":[
                                          {
                                             "name":"cid",
                                             "content":"10668"
                                          }
                                       ],
                                       "item":[
                                          {
                                             "id":"0-1-0-0-2-1-0",
                                             "text":"Add New List",
                                             "userdata":[
                                                {
                                                   "name":"clid",
                                                   "content":99999
                                                },
                                                {
                                                   "name":"cid",
                                                   "content":"10668"
                                                }
                                             ]
                                          }
                                       ]
                                    }
                                 ]
                              }
                           ]
                        }
                     ]
                  }
               ]
            }
         ]
      }
   ]
}

I need to iterate through all levels and match by "id" and get the value for "content".

var id = "0-1-0-0-0";

If id of JSON matches id then return the value for content. Should return 10667,10668

Thanks in advance!

2
  • 2
    and what have you tried? Commented Mar 29, 2016 at 21:30
  • Tried quite a few things but nothing gets even close to the output needed. The closest I got was console.log(getObjects(json,'id',"0-1-0-0-0")); but can't get the content to return with it. Commented Mar 29, 2016 at 23:45

1 Answer 1

1

Basically you can iterate over all elements in the object and stop the iteration if the wanted id is found.

Whats this question makes so problematic, is the half level of the first level, with id like 1 (level 0), 1- (level 1) and 1-0 (level 2) and the the next levels all separated by dashes.

This proposal uses String#indexOf and checks the path with a wanted index of 0 and iterates over if that check is true. If the full path is found the it returns the object. If not found the iteration and recursion goes on.

function getObject(object, path) {
    function getPart(o) {
        if (!path.indexOf(o.id)) {
            if (path === o.id) {
                result = o;
                return true;
            }
            return Array.isArray(o.item) && o.item.some(getPart);
        }
        return false;
    }

    var parts = path.split('-'),
        result;

    getPart(object);
    return result;
}

var object = { "id": 0, "item": [{ "id": "0-", "text": "BlueWing", "userdata": [{ "name": "cid", "content": "10377" }], "item": [{ "id": "0-0", "text": "Auto", "userdata": [{ "name": "cid", "content": "10377,10376,10378,10387,10379,10384,10386,10380,10383,10385,10381,10388,10533" }], "item": [{ "id": "0-0-0", "text": "Auto", "userdata": [{ "name": "cid", "content": "10377,10376,10378,10387,10379,10384,10386,10380,10383,10385,10381,10388" }], "item": [{ "id": "0-0-0-0", "text": "testaccount", "userdata": [{ "name": "cid", "content": "10377,10376,10378,10387,10379,10384,10386,10380,10383,10385,10381,10388" }], "item": [{ "id": "0-0-0-0-0", "text": "Add New List", "userdata": [{ "name": "clid", "content": 99999 }, { "name": "cid", "content": "10377,10376,10378,10387,10379,10384,10386,10380,10383,10385,10381,10388" }] }, { "id": "0-0-0-0-1", "text": "testaccount", "userdata": [{ "name": "cid", "content": "10377,10376,10378,10387,10379,10384,10386,10380,10383,10385,10381,10388" }], "item": [{ "id": "0-0-0-0-1-0", "text": "Add New List", "userdata": [{ "name": "clid", "content": 99999 }, { "name": "cid", "content": "10377,10376,10378,10387,10379,10384,10386,10380,10383,10385,10381,10388" }] }, { "id": "0-0-0-0-1-1", "text": "testaccount - 10377,10376,10378,10387,10379,10384,10386,10380,10383,10385,10381,10388", "userdata": [{ "name": "cid", "content": "10377,10376,10378,10387,10379,10384,10386,10380,10383,10385,10381,10388" }], "item": [{ "id": "0-0-0-0-1-1-0", "text": "Add New List", "userdata": [{ "name": "clid", "content": 99999 }, { "name": "cid", "content": "10377,10376,10378,10387,10379,10384,10386,10380,10383,10385,10381,10388" }] }] }] }] }] }, { "id": "0-0-1", "text": "Balise Toyota", "userdata": [{ "name": "cid", "content": "10533" }], "item": [{ "id": "0-0-1-0", "text": "Balise Toyota", "userdata": [{ "name": "cid", "content": "10533" }], "item": [{ "id": "0-0-1-0-0", "text": "Add New List", "userdata": [{ "name": "clid", "content": 99999 }, { "name": "cid", "content": "10533" }] }, { "id": "0-0-1-0-1", "text": "Balise Toyota", "userdata": [{ "name": "cid", "content": "10533" }], "item": [{ "id": "0-0-1-0-1-0", "text": "Add New List", "userdata": [{ "name": "clid", "content": 99999 }, { "name": "cid", "content": "10533" }] }, { "id": "0-0-1-0-1-1", "text": "Balise Toyota - 10533", "userdata": [{ "name": "cid", "content": "10533" }], "item": [{ "id": "0-0-1-0-1-1-0", "text": "Add New List", "userdata": [{ "name": "clid", "content": 99999 }, { "name": "cid", "content": "10533" }] }, { "id": "0-0-1-0-1-1-1", "text": "Balise.List - 11949", "userdata": [{ "name": "clid", "content": 11949 }, { "name": "cid", "content": "10533" }] }] }] }] }] }] }, { "id": "0-1", "text": "BlueWing", "userdata": [{ "name": "cid", "content": "10667,10668" }], "item": [{ "id": "0-1-0", "text": "HVAC", "userdata": [{ "name": "cid", "content": "10667,10668,10348,10294,10297,10313,10521,10351,10309,10296,10316,10315,10312,10301,10347,10300,10306,10308,10307,10298,10305,10371,10291,10349,10311,10354,10345,10310,10292,10372,10295,10304,10303,10299,10314,10290,10490,10293,10302" }], "item": [{ "id": "0-1-0-0", "text": "HVAC", "userdata": [{ "name": "cid", "content": "10667,10668" }], "item": [{ "id": "0-1-0-0-0", "text": "Add New List", "userdata": [{ "name": "clid", "content": 99999 }, { "name": "cid", "content": "10667,10668" }] }, { "id": "0-1-0-0-1", "text": "Mann Plumbing & Heating", "userdata": [{ "name": "cid", "content": "10667" }], "item": [{ "id": "0-1-0-0-1-0", "text": "Add New List", "userdata": [{ "name": "clid", "content": 99999 }, { "name": "cid", "content": "10667" }] }, { "id": "0-1-0-0-1-1", "text": "Mann Plumbing & Heating - 10667", "userdata": [{ "name": "cid", "content": "10667" }], "item": [{ "id": "0-1-0-0-1-1-0", "text": "Add New List", "userdata": [{ "name": "clid", "content": 99999 }, { "name": "cid", "content": "10667" }] }, { "id": "0-1-0-0-1-1-1", "text": "Radius List - 12960", "userdata": [{ "name": "clid", "content": 12960 }, { "name": "cid", "content": "10667" }] }] }] }, { "id": "0-1-0-0-2", "text": "RES Air Conditioning", "userdata": [{ "name": "cid", "content": "10668" }], "item": [{ "id": "0-1-0-0-2-0", "text": "Add New List", "userdata": [{ "name": "clid", "content": 99999 }, { "name": "cid", "content": "10668" }] }, { "id": "0-1-0-0-2-1", "text": "RES Air Conditioning - 10668", "userdata": [{ "name": "cid", "content": "10668" }], "item": [{ "id": "0-1-0-0-2-1-0", "text": "Add New List", "userdata": [{ "name": "clid", "content": 99999 }, { "name": "cid", "content": "10668" }] }] }] }] }] }] }] }] },
    id = "0-1-0-0-0",
    part = getObject(object, id);

document.write('<pre>' + JSON.stringify(part, 0, 4) + '</pre>');

Sign up to request clarification or add additional context in comments.

1 Comment

Brilliant! Worked like a charm. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.