let recursionData = [
{
id: 1,
name: "一级",
children: [
{
id: 2,
name: "二级-1",
children: [
{
id: 7,
name: "三级-1",
children: [
{
id: 10,
name: "四级-1",
},
],
},
{
id: 8,
name: "三级-2",
},
],
},
{
id: 3,
name: "二级-2",
children: [
{
id: 5,
name: "三级-3",
},
{
id: 6,
name: "三级-4",
},
],
},
{
id: 4,
name: "二级-3",
children: [
{
id: 9,
name: "三级-5",
children: [
{
id: 11,
name: "四级-2",
},
],
},
],
},
],
},
],
arr = [];
function funRecursion(list) {
for (let i in list) {
arr.push({
id: list[i].id,
name: list[i].name,
});
if (list[i].children) {
funRecursion(list[i].children);
}
}
return arr;
}
console.log(funRecursion(recursionData));
console.log(arr.map((item) => item.id).sort((a, b) => a - b));
|