var list = [
{title:"xxxx",level:0},
{title:"xxxx",level:0},
{title:"xxxx",level:1},
{title:"xxxx",level:1},
{title:"xxxx",level:1},
{title:"xxxx",level:2},
{title:"xxxx",level:2},
{title:"xxxx",level:1},
{title:"xxxx",level:1},
{title:"xxxx",level:0},
{title:"xxxx",level:1},
{title:"xxxx",level:1},
{title:"xxxx",level:1},
{title:"xxxx",level:1},
{title:"xxxx",level:0},
{title:"xxxx",level:1},
{title:"xxxx",level:1},
{title:"xxxx",level:1},
{title:"xxxx",level:1},
{title:"xxxx",level:0},
{title:"xxxx",level:1},
{title:"xxxx",level:1},
{title:"xxxx",level:2},
{title:"xxxx",level:0},
];
这种数据结构,转换为下面这种结构的:
[
{
title:"xxxx",
items:[]
},
{
title:"xxxx",
items:[
{
title:"xxxx",
items:[]
},
{
title:"xxxx",
items:[]
},
{
title:"xxxx",
items:[
{
title:"xxxx",
items:[]
},
]
},
]
}
...
]
level代表层级,1属于上一个0的子集,2属于上一个1的子集。。。可能会有N级
谁能帮忙写出这个转换函数?
有\r\n是因为控制台复制了换行。我刚转js,语法用的es6标准。