-1

how to get the respective nested JSON object based on Id. For example below is my complete JSON.

[
  {
    "id": 1,
    "title": "ASD Headquarters",
    "items": [
      {
        "id": 11,
        "title": "San Jose",
        "items": [
          {
            "id": 13,
            "title": "Jensen Chapman's Team",
            "items": [
              {
                "id": 14,
                "title": "Jimmy John"
              },
              {
                "id": 15,
                "title": "Daniel Mills"
              },
              {
                "id": 16,
                "title": "Chris Boden"
              }
            ]
          }
        ]
      },
      {
        "id": 12,
        "title": "Irvine",
        "items": [
          {
            "id": 23,
            "title": "Tracey Chapman's Team",
            "items": [
              {
                "id": 24,
                "title": "San Jesus"
              },
              {
                "id": 25,
                "title": "Fat Albert"
              },
              {
                "id": 26,
                "title": "Connor McDavid"
              }
            ]
          }
        ]
      },
      {
        "id": 30,
        "title": "San Diego",
        "items": [
          {
            "id": 31,
            "title": "Duran Duran's Team",
            "items": [
              {
                "id": 32,
                "title": "Amberlynn Pinkerton"
              },
              {
                "id": 33,
                "title": "Tony Mejia"
              },
              {
                "id": 34,
                "title": "Richard Partridge"
              },
              {
                "id": 35,
                "title": "Elliot Stabler"
              }
            ]
          },
          {
            "id": 40,
            "title": "Steely Dan's Team",
            "items": [
              {
                "id": 36,
                "title": "Tony Stark"
              },
              {
                "id": 37,
                "title": "Totally Rad"
              },
              {
                "id": 38,
                "title": "Matt Murdock"
              },
              {
                "id": 39,
                "title": "Stan Lee"
              }
            ]
          }
        ]
      }
    ]
  }
]

From the above json how do i filter only particular nested object which have id as 11 => {"id": 11} using underscore.js Output which i required is :

{
"id":11, "title":"San Jose", "items":[
{
"id":13, "title":"Jensen Chapman's Team", "items":[
{
"id":14, "title":"Jimmy John" }, {
"id":15, "title":"Daniel Mills" }, {
"id":16, "title":"Chris Boden" } ] } ] }
Vijay Baskaran
  • 691
  • 2
  • 7
  • 18
  • [Calling a javascript function recursively](https://stackoverflow.com/questions/7065120/calling-a-javascript-function-recursively) – Ele Mar 07 '18 at 18:05

1 Answers1

2

You can use a recursive algorithm to look for an object in the current array as well as the nested ones.

var data = [{"id":1,"title":"ASD Headquarters","items":[{"id":11,"title":"San Jose","items":[{"id":13,"title":"Jensen Chapman's Team","items":[{"id":14,"title":"Jimmy John"},{"id":15,"title":"Daniel Mills"},{"id":16,"title":"Chris Boden"}]}]},{"id":12,"title":"Irvine","items":[{"id":23,"title":"Tracey Chapman's Team","items":[{"id":24,"title":"San Jesus"},{"id":25,"title":"Fat Albert"},{"id":26,"title":"Connor McDavid"}]}]},{"id":30,"title":"San Diego","items":[{"id":31,"title":"Duran Duran's Team","items":[{"id":32,"title":"Amberlynn Pinkerton"},{"id":33,"title":"Tony Mejia"},{"id":34,"title":"Richard Partridge"},{"id":35,"title":"Elliot Stabler"}]},{"id":40,"title":"Steely Dan's Team","items":[{"id":36,"title":"Tony Stark"},{"id":37,"title":"Totally Rad"},{"id":38,"title":"Matt Murdock"},{"id":39,"title":"Stan Lee"}]}]}]}];

console.log(find(12, data));

function find(id, [head, ...tail]) {
  if (!head)
    return null;

  return checkObj(id, head) || find(id, tail);
}

function checkObj(id, obj) {
  return obj.id === id ? obj : find(id, obj.items || [])
}

This also uses parameter destructuring in order to conveniently separate the "head" of the array from its "tail".


It could also be done within a single function.

var data = [{"id":1,"title":"ASD Headquarters","items":[{"id":11,"title":"San Jose","items":[{"id":13,"title":"Jensen Chapman's Team","items":[{"id":14,"title":"Jimmy John"},{"id":15,"title":"Daniel Mills"},{"id":16,"title":"Chris Boden"}]}]},{"id":12,"title":"Irvine","items":[{"id":23,"title":"Tracey Chapman's Team","items":[{"id":24,"title":"San Jesus"},{"id":25,"title":"Fat Albert"},{"id":26,"title":"Connor McDavid"}]}]},{"id":30,"title":"San Diego","items":[{"id":31,"title":"Duran Duran's Team","items":[{"id":32,"title":"Amberlynn Pinkerton"},{"id":33,"title":"Tony Mejia"},{"id":34,"title":"Richard Partridge"},{"id":35,"title":"Elliot Stabler"}]},{"id":40,"title":"Steely Dan's Team","items":[{"id":36,"title":"Tony Stark"},{"id":37,"title":"Totally Rad"},{"id":38,"title":"Matt Murdock"},{"id":39,"title":"Stan Lee"}]}]}]}];

console.log(find(12, data));

function find(id, [head, ...tail]) {
  if (!head)
    return null;

  if (head.id === id)
    return head;
    
  return find(id, head.items || []) || find(id, tail);
}
  • @VijayBaskaran: I got the conditional expressions mixed up. It's now fixed to be working and clearer. –  Mar 07 '18 at 18:28