比如原始数据:
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....},...]},
]
谢谢!
相关分类