簡體   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