在Node.js

时间:2017-12-11 03:46:30

标签: javascript arrays node.js recursion

以下是我的数组

[
    {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
}

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

2 个答案:

答案 0 :(得分:2)

如果有人想根据 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);

答案 1 :(得分:0)

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

  • getNestedChildren会返回一系列子项,因此请将out重命名为children
  • 递归调用返回的子节点是在调用中处理的父节点的孙子节点。因此,请调用递归调用grandchildren的结果。

导致代码无效的问题:

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

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

&#13;
&#13;
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);
&#13;
&#13;
&#13;