数据结构
arr: [
{ id: 1, icon: "user", name: "张安", hidden: true },
{
id: 2,
icon: null,
name: "张三",
hidden: false,
children: [
{
id: 4,
name: "张三2",
icon: "",
type: 0,
level: 1,
},
{
id: 5,
name: "张三3",
icon: "",
type: 0,
level: 2,
},
],
},
],
js代码
let newarr = [];
this.arr.forEach((v, index) => {
newarr.push({
index: v.index,
name: v.name,
});
if (v.children && v.children.length > 0) {
let dd = [];
v.children.forEach((item) => {
dd.push({
index: item.id,
name: item.name,
});
});
newarr[index].children = dd;
}
});
运行结果