如何根据id获取相应的JSON对象

时间:2018-03-07 18:02:38

标签: javascript underscore.js

如何根据Id获取相应的嵌套JSON对象。例如,下面是我的完整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"
              }
            ]
          }
        ]
      }
    ]
  }
]

从上面的json我怎么只过滤id为11 =>的特定嵌套对象? {“id”:11}使用underscore.js 我要求的输出是:

{
"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" } ] } ] }

1 个答案:

答案 0 :(得分:2)

您可以使用递归算法在当前数组中查找对象以及嵌套对象。

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 || [])
}

这也使用参数解构,以便方便地将数组的“头部”与其“尾部”分开。

也可以在一个功能中完成。

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);
}