简体   繁体   English

对象中的多维数组

[英]Multi-Dimensional Array within Object

Please go through this javascript object: 请仔细阅读这个javascript对象:

var obj = [{
  id: "A",
  children: [{
    id: "B",
    children: [{
      id: "C",
      children: [{
        id: "D",
        children: [{
          id: "E",
          children: [{
            id: "F"
          }]
        }]
      }, {
        id: "G",
        children: {
          id: "H"
        }
      }]
    }, {
      id: "I"
    }]
  }, {
    id: "J",
    children: [{
      id: "K"
    }]
  }]
}, {
  id: "L"
}, {
  id: "M",
  children: {
    id: "N",
    children: [{
      id: "O"
    }]
  }
}, {
  id: "P"
}];

How to write JavaScript code to recursively parse it and print all the IDs in console so that the output looks like: 如何编写JavaScript代码以递归方式解析它并在控制台中打印所有ID,以便输出如下所示:

A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P

This is how far I could reach. 这是我能达到的目标。 I couldn't think of any logic after that. 之后我想不出任何逻辑。

for ( i=0 ; i < obj.length ; i++ ){
         var objId = obj[i];
         for( j=i; j<1 ; j++){
             console.log(obj[j].id);
             console.log(obj[j].children[j].id);
         }
     }

I don't understand what logic should be applied here. 我不明白应该在这里应用什么逻辑。 Do help. 帮忙。

You could use an iterative and recursive approach with a depth-first search algorithm. 您可以使用深度优先搜索算法的迭代和递归方法。

Edit: Extended for children as object. 编辑:为子对象扩展。

 var data = [{ id: "A", children: [{ id: "B", children: [{ id: "C", children: [{ id: "D", children: [{ id: "E", children: [{ id: "F" }] }] }, { id: "G", children: { id: "H" } }] }, { id: "I" }] }, { id: "J", children: [{ id: "K" }] }] }, { id: "L" }, { id: "M", children: { id: "N", children: [{ id: "O" }] } }, { id: "P" }]; data.forEach(function iter(a) { console.log(a.id); if (Array.isArray(a.children)) { a.children.forEach(iter); return; } if (a.children && typeof a.children === 'object') { // omit this part iter(a.children); // if children is } // always an array }); 

This version collects all necessary data and returns it in an array. 此版本收集所有必需的数据并将其返回到数组中。

 var data = [{ id: "A", children: [{ id: "B", children: [{ id: "C", children: [{ id: "D", children: [{ id: "E", children: [{ id: "F" }] }] }, { id: "G", children: { id: "H" } }] }, { id: "I" }] }, { id: "J", children: [{ id: "K" }] }] }, { id: "L" }, { id: "M", children: { id: "N", children: [{ id: "O" }] } }, { id: "P" }], result = data.reduce(function iter(r, o) { r.push(o.id); if (Array.isArray(o.children)) { return o.children.reduce(iter, r); } if (o.children && typeof o.children === 'object') { // omit this part return iter(r, o.children); // if children is } // always an array return r; }, []); console.log(result); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

You could use the below ES6 function. 您可以使用以下ES6功能。 Note that at two places you did not define children as an array, which I assume is a mistake. 请注意,在两个地方你没有将children元素定义为数组,我认为这是一个错误。 If it is indented, I would strongly advise to reconsider, and make it consistent throughout. 如果它是缩进的,我强烈建议重新考虑,并使其始终保持一致。

 function getIds(data) { return data.reduce((acc, el) => acc.concat(el.id, getIds(el.children || [])), []) } var obj = [{ id: "A", children: [{ id: "B", children: [{ id: "C", children: [{ id: "D", children: [{ id: "E", children: [{ id: "F" }] }] }, { id: "G", children: [{ id: "H" }] }] }, { id: "I" }] }, { id: "J", children: [{ id: "K" }] }] }, { id: "L" }, { id: "M", children: [{ id: "N", children: [{ id: "O" }] }] }, { id: "P" }]; console.log(getIds(obj).join('\\n')); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

What you are showing looks like it is asking for a depth first solution, as the order of the ids are clearly alphabetical and they are ordered by first encountered then by depth. 您所展示的内容看起来像是要求深度优先解决方案,因为ID的顺序清楚地按字母顺序排列,并且它们先按深度排序然后按顺序排序。

As a result, every id encountered will be collected, and then the deeper ids will be examined. 结果,将收集遇到的每个id,然后将检查更深的ID。 This can, and probably should, be done with recursion. 这可以,也可能应该通过递归来完成。

Here is an example. 这是一个例子。

 var obj=[{id:"A",children:[{id:"B",children:[{id:"C",children:[{id:"D",children:[{id:"E",children:[{id:"F"}]}]},{id:"G",children:{id:"H"}}]},{id:"I"}]},{id:"J",children:[{id:"K"}]}]},{id:"L"},{id:"M",children:{id:"N",children:[{id:"O"}]}},{id:"P"}]; var ids = [];//output holder (function tree(cur){ //recursive function for(var i = 0; i < cur.length; i++){ //iterate current set ids.push(cur[i].id); //always store id if(cur[i].children) tree(cur[i].children); //recurse if children exist } })(obj) //start from the top console.log(ids); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM