找出isshow为true的对象的id???
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body></body>
<script>
let array = [
{
id:1,
isshow:false,
children:[
{
id:2,
isshow:false,
children:[
{
id:21,
isshow:true,
children:[]
}
]
}
]
},
{
id:3,
isshow:false,
children:[]
},
{
id:4,
isshow:true,
children:[
{
id:5,
isshow:false,
children:[
{
id:6,
isshow:true,
children:[]
},
{
id:7,
isshow:false,
children:[]
},
{
id:8,
isshow:true,
children:[]
},
]
}
]
}
]
var returnItem="";
function find (arr,isshow){
if (!arr) {
return
}
arr.forEach((item)=>{
if(item.isshow==isshow){
returnItem +=item.id+",";
if(item.children.length>0){
find(item.children,isshow)
}
}else{
find(item.children,isshow)
}
})
}
find(array,true)
console.log("returnItem",returnItem) ===============================结果为:21,4,6,8,
</script>
</html>