小编Sah*_*aja的帖子

在 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}}
]
Run Code Online (Sandbox Code Playgroud)

我想要像这样嵌套的对象作为输出:

[
    {id: 1, title: 'hello', parent: 0, children: [
        {id: 3, title: 'hello', parent: 1, children: [
            {id: 4, title: 'hello', parent: 3, children: [
                {id: 5, title: …
Run Code Online (Sandbox Code Playgroud)

javascript arrays recursion node.js

5
推荐指数
1
解决办法
3666
查看次数

标签 统计

arrays ×1

javascript ×1

node.js ×1

recursion ×1