繁体   English   中英

在 Node.js 中递归创建嵌套数组

[英]Create a nested array recursively in Node.js

以下是我的数组

[
    {id: 1, title: 'hello', parent: {number:0}},
    {id: 2, title: 'hello', parent: {number:0}},
    {id: 3, title: 'hello', parent: {number:1}},
    {id: 4, title: 'hello', parent: {number:3}},
    {id: 5, title: 'hello', parent: {number:4}},
    {id: 6, title: 'hello', parent: {number:4}},
    {id: 7, title: 'hello', parent: {number:3}},
    {id: 8, title: 'hello', parent: {number:2}}
]

我希望将这样嵌套的对象作为输出:

[
    {id: 1, title: 'hello', parent: 0, children: [
        {id: 3, title: 'hello', parent: 1, children: [
            {id: 4, title: 'hello', parent: 3, children: [
                {id: 5, title: 'hello', parent: 4},
                {id: 6, title: 'hello', parent: 4}
            ]},
            {id: 7, title: 'hello', parent: 3}
        ]}
    ]},
    {id: 2, title: 'hello', parent: 0, children: [
        {id: 8, title: 'hello', parent: 2}
    ]}
]

请帮助我使用递归函数在 node.js 中执行此操作。

以下是我尝试过的递归函数:

    function getNestedChildren(arr, parent) {
    var out = []
    for(var i in arr) {
        if(arr[i].parent.number == parent.number) {
            var children = getNestedChildren(arr, arr[i].id)

            if(children.length) {
                arr[i].children = children
            }
            out.push(arr[i])
        }
    }
    return out
}

请帮我解决这个问题。 我是这方面的新手。

重命名一些变量帮助我解决了这个问题。

  • getNestedChildren返回儿童的数组,所以重命名outchildren
  • 递归调用返回的子代是调用中正在处理的父代的孙代。 因此,将递归调用的结果调用为grandchildren

导致代码不工作的问题:

  • 已发布代码的第 4 行使用了 parent 参数的id属性。 因此,要么确保对getNestedChidren每次调用getNestedChidren提供具有此类属性的对象(如下所示),要么将第二个参数更改为parentNumber并仅提供number属性的数值。 你的选择。

最后,避免for ... in循环中使用for ... in来迭代数组 - 请进行网络搜索以获取更多信息和讨论。

 var array = [ {id: 1, title: 'hello', parent: {number:0}}, {id: 2, title: 'hello', parent: {number:0}}, {id: 3, title: 'hello', parent: {number:1}}, {id: 4, title: 'hello', parent: {number:3}}, {id: 5, title: 'hello', parent: {number:4}}, {id: 6, title: 'hello', parent: {number:4}}, {id: 7, title: 'hello', parent: {number:3}}, {id: 8, title: 'hello', parent: {number:2}} ] function getNestedChildren(arr, parent) { var children = []; for(var i =0; i < arr.length; ++i) { if(arr[i].parent.number == parent.number) { var grandChildren = getNestedChildren(arr, {number: arr[i].id}) if(grandChildren.length) { arr[i].children = grandChildren; } children.push( arr[i]); } } return children; } var nest = getNestedChildren(array,{number: 0}); console.log( nest);

如果有人想知道带有扁平初始数组的解决方案,请按照 user992731 的请求:

 var array = [ {id: 1, title: 'hello', parent: 0}, {id: 2, title: 'hello', parent: 0}, {id: 3, title: 'hello', parent: 1}, {id: 4, title: 'hello', parent: 3}, {id: 5, title: 'hello', parent: 4}, {id: 6, title: 'hello', parent: 4} ]; function getNestedChildren(arr, parent) { var children = []; for(var i =0; i < arr.length; ++i) { if(arr[i].parent == parent) { var grandChildren = getNestedChildren(arr, arr[i].id); if(grandChildren.length) { arr[i].children = grandChildren; } children.push(arr[i]); } } return children; } var nest = getNestedChildren(array,0); console.log(nest);

暂无
暂无

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

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