let listArr = [
{
id: 1,
name: "A",
childrenLabelInfo: [
{
id: 2,
name: "B",
childrenLabelInfo: [
{
id: 3,
name: "C"
},
{
id: 4,
name: "D"
}
]
},
{
id: 5,
name: "E"
}
]
},
{
id: 6,
name: "F",
childrenLabelInfo: [
{
id: 7,
name: "G",
childrenLabelInfo: [
{
id: 8,
name: "H"
},
{
id: 9,
name: "I"
}
]
},
{
id: 10,
name: "G"
}
]
}
]
let arrList = []
listArr.forEach(item => {
ArrItem(item, arrList)
})
function ArrItem(arrItem, arrLists) { // 保存第一层数据
let arr = [];
arr.push(arrItem.id);
if (arrItem.childrenLabelInfo !== undefined) {
arrItem.childrenLabelInfo.forEach((item, index) => {
// 深拷贝,转换引用类型地址
let arrStyle = JSON.parse(JSON.stringify(arr))
arrLists.push(Return(arrStyle, item, index, arrLists))
})
} else {
arrLists.push(arr)
}
}
function Return(arr, arrItem, index, arrLists) { // 处理其他层次的函数
arr.push(arrItem.id)
if (arrItem.childrenLabelInfo !== undefined) {
arrItem.childrenLabelInfo.forEach((item, ind) => {
let Star = JSON.parse(JSON.stringify(arr))
arrLists.push(Return(Star, item, ind, arrLists))
})
} else {
return arr
}
}
let arrTrue = []
arrList.forEach(item => {
// 处理无数据情况的undefined
if (item !== undefined) {
arrTrue.push(item)
}
})
console.log(arrTrue);