我在API响应中收到一个对象数组,并且是嵌套数组。我希望通过ID找到一个对象

时间:2015-10-21 06:52:54

标签: javascript arrays angularjs

您好,这是一个从API响应接收的对象数组,我正在使用angular.js和javascript,我希望通过id获取对象,ID为“ 1_1_1 ”。这是可能?我坚持下去了。请帮忙。

[
      {
        "text": "folder 1",
        "parent": {
          "id": "1",
          "name": "folder 1"
        },
        "children": [
          {
            "text": "folder 1_1",
            "parent": {
              "id": "1_1",
              "name": "folder 1_1"
            },
            "children": [
              {
                "text": "folder 1_1_1",
                "parent": {
                  "id": "1_1_1",
                  "name": "folder 1_1_1"
                },
                "children": []
              },
              {
                "text": "folder 1_1_2",
                "parent": {
                  "id": "1_1_2",
                  "name": "folder 1_1_2"
                },
                "children": []
              }
            ]
          },
          {
            "text": "folder 1_2",
            "parent": {
              "id": "1_2",
              "name": "folder 1_2"
            },
            "children": [
              {
                "text": "folder 1_2_1",
                "parent": {
                  "id": "1_2_1",
                  "name": "folder 1_2_1"
                },
                "children": []
              }
            ]
          }
        ]
      },
      {
        "text": "folder 2",
        "parent": {
          "id": "2",
          "name": "folder 2"
        },
        "children": [
          {
            "text" : "folder 2_1",
            "parent": {
              "id": "2_1",
              "name": "folder 2_1"
            },
            "children": []
          },
          {
            "text" : "folder 2_2",
            "parent": {
              "id": "2_2",
              "name": "folder 2_2"
            },
            "children": []
          },
          {
            "text" : "folder 2_3",
            "parent": {
              "id": "2_3",
              "name": "folder 2_3"
            },
            "children": [
              {
                "text" : "folder 2_3_1",
                "parent": {
                  "id": "2_3_1",
                  "name": "folder 2_3_1"
                },
                "children": []
              }
            ]
          },
        ]
      },
      {
       "text": "folder 3",
       "parent": {
         "id": "3",
         "name": "folder 3"
       },
       "children": []
     }
    ]

2 个答案:

答案 0 :(得分:1)

试试这个:

function isMatch(element, criteria) {
    var parent = null;
    if (typeof element.parent !== 'undefined') {
        parent = element.parent;
        if (typeof parent.id !== 'undefined') {
            return (parent.id === criteria);
        }
    }
    return false;
}

function findById (tree, criteria) {

    for (var i = 0; i < tree.length; i = i + 1) {
        var obj = tree[i];
        if (isMatch(obj, criteria)) {
            return obj;
        };

        if (typeof obj.children !== 'undefined') {
            var foundElement = findById(obj.children, criteria);
            if (foundElement != null) {
                return foundElement;
            }
        }
    }

    return null;
}

答案 1 :(得分:0)

JavaScript或Angular中没有内置函数来执行此操作。

然而,这是一个很容易通过递归解决的问题:

function findById(id, items) {
  for (var i = 0; i < items.length; i++) {
    if (items[i].id === id) {
      return items[i];
    }

    var result = findById(id, items[i].children);
    if (result !== null) {
      return result; 
    }
  }
  return null;
}