//JSRUN引擎2.0,支持多达30种语言在线运行,全仿真在线交互输入输出。
const data = [ {
id: 1,
title: "课程 1",
children: [ { id: 4, title: "课程 1-1" },
{
id: 5,
title: "课程 1-2",
children: [ { id: 6, title: "课程 1-2-1" },
{ id: 7, title: "课程 1-2-2" },
],
},
],
},
{ id: 2, title: "课程 2" },
{ id: 3, title: "课程 3" },
];
function flatten(data){
return data.reduce((arr,{id,title,children=[]})=>{
return arr.concat([{id,title}],flatten(children))
},[])
}
flatten(data)
console.log(flatten(data))