Javascript 从嵌套数组生成祖先列表

Javascript 从嵌套数组生成祖先列表,javascript,arrays,multidimensional-array,Javascript,Arrays,Multidimensional Array,嵌套数组如下所示: var arr=[{ id:2, 名称:‘a’, 儿童:[] }, { id:5, 名称:‘b’, 儿童:[{ 身份证号码:14, 名称:“b2” }] }, { 身份证号码:15, 名称:“d”, 儿童:[] }]; 您可以移交一个作为父对象的对象,并搜索想要的id 函数getParentobject,id{ var结果; object.children | |[]someo=>result=o.id==id?对象:getParento,id; 返回结果; } var数

嵌套数组如下所示:

var arr=[{ id:2, 名称:‘a’, 儿童:[] }, { id:5, 名称:‘b’, 儿童:[{ 身份证号码:14, 名称:“b2” }] }, { 身份证号码:15, 名称:“d”, 儿童:[]
}]; 您可以移交一个作为父对象的对象,并搜索想要的id

函数getParentobject,id{ var结果; object.children | |[]someo=>result=o.id==id?对象:getParento,id; 返回结果; } var数组=[{id:2,名称:'a',子项:[]},{id:5,名称:'b',子项:[{id:14,名称:'b2'}]},{id:15,名称:'d',子项:[]}; loggetParent{children:array},14;
.作为控制台包装器{max height:100%!important;top:0;}您可以移交作为父对象的对象,并递归搜索想要的id

函数getParentobject,id{ var结果; object.children | |[]someo=>result=o.id==id?对象:getParento,id; 返回结果; } var数组=[{id:2,名称:'a',子项:[]},{id:5,名称:'b',子项:[{id:14,名称:'b2'}]},{id:15,名称:'d',子项:[]}; loggetParent{children:array},14;
.as console wrapper{max height:100%!important;top:0;}如果我们可以假设只有两个级别存在父级和子级,而不是子级的子级,那么下面的函数findAncestor会满足您的需要。它迭代所有父元素并检查它们是否有具有相关ID的子元素

function findAncestor(id) {
  for (let i = 0; i < arr.length; i++) {
    let obj = arr[i];
    if (obj.hasOwnProperty('children')) {
      if (obj.children.length > 0) {
        for (let j = 0; j < obj.children.length; j++) {
          if (obj.children[j].id === id) {
            return obj;
          }
        }
      }
    }
  }

  return null;
}

console.info(findAncestor(14));

如果您需要处理具有相同ID的子级可能出现在多个父级中的情况,则应使用结果数组并将找到的所有结果添加到该数组中,然后再最终返回该数组。

如果我们可以假设只有两个级别存在父级和子级,而不是子级的子级,则以下函数findAncestor可以满足您的需要。它迭代所有父元素并检查它们是否有具有相关ID的子元素

function findAncestor(id) {
  for (let i = 0; i < arr.length; i++) {
    let obj = arr[i];
    if (obj.hasOwnProperty('children')) {
      if (obj.children.length > 0) {
        for (let j = 0; j < obj.children.length; j++) {
          if (obj.children[j].id === id) {
            return obj;
          }
        }
      }
    }
  }

  return null;
}

console.info(findAncestor(14));
如果您需要处理具有相同ID的子项可能出现在多个父项中的情况,则应使用结果数组并将找到的所有结果添加到该数组中,然后最终返回。

您可以尝试这种方法

     var arr = [{
          id: 2,
          name: 'a',
          children: []
        }, {
          id: 5,
          name: 'b',
          children: [{ 
            id: 14,
            name: 'b2'
          }]

        }, {
          id: 15,
          name: 'd',
          children: []
        }];


    function getAncestor(obj,id,ancestor){
     if(obj.id===id){
        console.log(ancestor);
     }
     else
        if(obj&& obj.children && obj.children.length)
            obj.children.forEach(item=>this.getAncestor(item,id,obj));
    }

arr.forEach(o=>getAncestor(o,14,{}));
你可以这样试试

     var arr = [{
          id: 2,
          name: 'a',
          children: []
        }, {
          id: 5,
          name: 'b',
          children: [{ 
            id: 14,
            name: 'b2'
          }]

        }, {
          id: 15,
          name: 'd',
          children: []
        }];


    function getAncestor(obj,id,ancestor){
     if(obj.id===id){
        console.log(ancestor);
     }
     else
        if(obj&& obj.children && obj.children.length)
            obj.children.forEach(item=>this.getAncestor(item,id,obj));
    }

arr.forEach(o=>getAncestor(o,14,{}));

深度优先搜索算法-获取父节点为:

function getParentNodeByKey(obj, targetId, paramKey) {
    if (obj.children) {
        if (obj.children.some(ch => ch[paramKey] === targetId))
            return obj;
        else {
            for (let item of obj.children) {
                let check = this.getParentNodeByKey(item, targetId, paramKey)
                if (check) {
                    return check;
                }
            }
        }
    }
    return null
}
和要测试的代码:

var arr = [{
    id: 2,
    name: 'a',
    children: []
  }, {
    id: 5,
    name: 'b',
    children: [{
      id: 14,
      name: 'b2'
    }]

  }, {
    id: 15,
    name: 'd',
    children: []
}];

let parentElement;
const valueToFind = 14;
const desiredkey = 'id';
for (let i = 0; i < arr.length; i++) {
    parentElement = getParentNodeByKey(arr[i], valueToFind, desiredkey);
    if(parentElement)
        break;
}

console.log(`The parent element is:`, parentElement);

深度优先搜索算法-获取父节点为:

function getParentNodeByKey(obj, targetId, paramKey) {
    if (obj.children) {
        if (obj.children.some(ch => ch[paramKey] === targetId))
            return obj;
        else {
            for (let item of obj.children) {
                let check = this.getParentNodeByKey(item, targetId, paramKey)
                if (check) {
                    return check;
                }
            }
        }
    }
    return null
}
和要测试的代码:

var arr = [{
    id: 2,
    name: 'a',
    children: []
  }, {
    id: 5,
    name: 'b',
    children: [{
      id: 14,
      name: 'b2'
    }]

  }, {
    id: 15,
    name: 'd',
    children: []
}];

let parentElement;
const valueToFind = 14;
const desiredkey = 'id';
for (let i = 0; i < arr.length; i++) {
    parentElement = getParentNodeByKey(arr[i], valueToFind, desiredkey);
    if(parentElement)
        break;
}

console.log(`The parent element is:`, parentElement);

对于id:14,不是父id:5吗?是的,抱歉我更正了它对于id:14,不是父id:5吗?是的,抱歉我更正了它