递归:从数组javascript获取对象

时间:2019-02-08 06:01:14

标签: javascript arrays recursion

我有一个像这样的递归数组

var data ={
      "isRoot": true,
      "isParent": true,
      "name": "first",
      "id": 1,
      "child": [
        {
          "isRoot": false,
          "id": 2,
          "isParent": false,
          "name": "second",
          "child": [
            {
              "id": 4,
              "child": [],
              "name": "inner",
              "isParent": false,
              "isRoot": false
            }
          ]
        },
        {
          "isRoot": false,
          "id": 3,
          "isParent": true,
          "name": "third",
          "child": []
        }
      ]
    }

child是嵌套的,它可能在任何更深层次上都有,我想从与id相匹配的数组中获取单个对象。

例如,如果id=4应该返回该对象

    {
      "id": 4,
      "child": [],
      "name": "inner",
      "isParent": false,
      "isRoot": false
    }

如何在另一篇文章中找到我的内容

function flatten(data) {

    data.forEach(function (element){
        //how can i do the comparison here
        if(element.child.length) {
            flatten(element.child);
        } else {

        }
    });
}

flatten(data.child);

但是我不知道匹配逻辑,有没有人对此有解决方案?

3 个答案:

答案 0 :(得分:1)

您可以进行递归深度优先搜索:

function find(arr, id) {
  for (var e of arr) {
    if (e.id === id) return e;
    var found = find(e.child, id);
    if (found) return found;
  }
}

var data = {
  "isRoot": true,
  "isParent": true,
  "name": "first",
  "id": 1,
  "child": [{
      "isRoot": false,
      "id": 2,
      "isParent": false,
      "name": "second",
      "child": [{
        "id": 4,
        "child": [],
        "name": "inner",
        "isParent": false,
        "isRoot": false
      }]
    },
    {
      "isRoot": false,
      "id": 3,
      "isParent": true,
      "name": "third",
      "child": []
    }
  ]
};

var result = find([data], 4);
console.log(result)

请注意,我正在数组中传递顶级data对象以保持一致性。

答案 1 :(得分:0)

完成的代码,它也可以找到多个匹配项:

var data ={
      "isRoot": true,
      "isParent": true,
      "name": "first",
      "id": 1,
      "child": [
        {
          "isRoot": false,
          "id": 2,
          "isParent": false,
          "name": "second",
          "child": [
            {
              "id": 4,
              "child": [],
              "name": "inner",
              "isParent": false,
              "isRoot": false
            }
          ]
        },
        {
          "isRoot": false,
          "id": 3,
          "isParent": true,
          "name": "third",
          "child": []
        }
      ]
    }

function search(data,id) {

    var founds=[];
    data.forEach(function (element){
      
      if(element.id===id)
        founds.push(element);
      
        if(element.child.length) {
            founds=founds.concat(search(element.child,id));
        }
    });
    return founds;
}

console.log(search([data],3));

答案 2 :(得分:0)

您可以使用递归进行此操作。您检查对象是否具有child,您需要遍历子数组并找到ID为的obj。这是我的代码

var data ={
      "isRoot": true,
      "isParent": true,
      "name": "first",
      "id": 1,
      "child": [
        {
          "isRoot": false,
          "id": 2,
          "isParent": false,
          "name": "second",
          "child": [
            {
              "id": 4,
              "child": [],
              "name": "inner",
              "isParent": false,
              "isRoot": false
            }
          ]
        },
        {
          "isRoot": false,
          "id": 3,
          "isParent": true,
          "name": "third",
          "child": []
        }
      ]
    }

function find(id,obj){
	if(obj.id === id) return obj;
        //if the object has child
	if(obj.child){
                //iterating through child array
		for(let child of obj.child){
                        //call the find function again on eact child
			let childWithId = find(id,child)
                        //if there is some object in child with id
			if(childWithId){
				return childWithId;
			}
		}
	}
}
console.log(find(3,data));
console.log(find(4,data));
console.log(find(1,data));
console.log(find(2,data));