在Node.js中递归创建嵌套数组 [英] Create a nested array recursively in Node.js

查看:184
本文介绍了在Node.js中递归创建嵌套数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我的数组

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

,而我想像这样嵌套对象作为输出:

  [
{id :1,标题: hello,父母:0,孩子:[
{id:3,标题: hello,父母:1,孩子:[
{id:4,标题:' hello',父母:3,孩子:[
{id:5,标题: hello,父母:4},
{id:6,标题: hello,父母:4}
]},
{id:7,标题: hello,父级:3}
]}
]},
{id:2,标题: hello,父母:0,孩子:[
{id:8,标题: hello,父母:2}
]}
]

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

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

 函数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 =儿童
}
out.push(arr [i])
}
}
返回
}

请帮助我解决此问题。我是新手。

解决方案

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




  • getNestedChildren 返回一个子数组,因此将 out 重命名为 children

  • 递归调用返回的子代是该调用中正在处理的父代的孙代。因此,将递归调用的结果称为孙子



引起问题的原因代码无效:




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



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



  var array = [{{id:1,title:'hello',parent:{number:0}},{id:2,title:'hello',parent:{number:0}},{id:3,title : hello,父代:{number:1}},{id:4,标题: hello,父代:{number:3}},{id:5,标题: hello,父代:{number: 4}},{id:6,标题: hello,父:{number:4}},{id:7,标题: hello,父:{number:3}},{id:8,标题:'hello',父级:{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);  


Following is my 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}}
]

and I want to have objects nested like this as output :

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

Please help me with a recursive function to do this in node.js.

Following is the recursive function is what I have tried:

    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
}

Please help me to solve this. I am a newbie in this.

解决方案

Renaming some variables helped me solve this.

  • getNestedChildren returns an array of children, so rename out to children.
  • the children returned by the recursive call are the grandchildren of the parent being processed in the call. So call the result of the recursive call grandchildren.

The problem that caused the code to not work:

  • line 4 of the posted code uses the id property of the parent parameter. So either ensure that every call to getNestedChidren provides an object with such a property (as below), or change the second argument to parentNumber and just supply the numeric value of the number property. Your choice.

Lastly, avoid using for ... in loops to iterate an array - please do a web search for more information and discussion.

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);

这篇关于在Node.js中递归创建嵌套数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆