猿问

怎样效率高的生成树形结构的数据?

比如原始数据:


let data = [

    {id: 1, name:'a', parent_id: 0},

    {id: 2, name:'b', parent_id: 0},

    {id: 3, name:'a-1', parent_id: 1},

    {id: 4, name:'a-2', parent_id: 1},

    {id: 5, name:'b-1', parent_id: 2},

    {id: 6, name:'b-2', parent_id: 2},

    {id: 7, name:'a-1-1', parent_id: 3},

]

重构成:


let data2 = [

    {id: 1, name:'a', parent_id: 0, children: [{id: 3, name:'a-1', parent_id: 1....},...]},

    {id: 2, name:'b', parent_id: 0, children: [{id: 5, name:'b-1', parent_id: 2....},...]},

]

谢谢!


慕斯709654
浏览 463回答 1
1回答
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答