Breeze醉清风
2021-03-03 16:54:46浏览 1582
//对象嵌套对象
let List = [{
id: 1,
name: '1层',
child: {
id: 2,
name: '1-1层'
}
}, {
id: 3,
name: '2层'
}, {
id: 4,
name: '3层',
child: {
id: 5,
name: '3-1层',
child:{
id:6,
name:'3-1-1层'
}
}
}]
let newList=[]
List.forEach((item) =>{
aa(item)
})
function aa(ul){
if(ul.child){
newList.push(ul.name)
aa(ul.child)
}else{
newList.push(ul.name)
}
}
console.log(newList)
let List = [{
id: 1,
name: '1层',
child: [{
id: 2,
name: '1-1层',
child:[{
id: 2,
name: '1-1-1层'
},{
id: 2,
name: '1-2-2层'
},{
id: 2,
name: '1-3-3层'
}]
},{
id: 2,
name: '1-2层'
},{
id: 2,
name: '1-3层'
}]
}, {
id: 3,
name: '2层'
}, {
id: 4,
name: '3层',
child: [{
id: 2,
name: '3-1层'
},{
id: 2,
name: '3-2层'
},{
id: 2,
name: '3-3层'
}]
}]
let newList=[]
List.forEach((item) =>{
aa(item)
})
function aa(ul){
if(ul.child){
newList.push(ul.name)
ul.child.forEach((item) => {
aa(item)
})
}else{
newList.push(ul.name)
}
}
console.log(newList)