在多级数组对象中根据id取值,循环加递归2019.12.20
<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>递归</title>
</head>
<body>
<script>
var options = [{
value: 'zhinan',
label: '指南',
price: '100',
children: [{
value: 'shejiyuanze',
label: '设计原则',
price: '1002',
children: [{
value: 'yizhi',
label: '一致',
price: '1003——1',
children: [{
value: 'yizhi——11',
label: '一致',
price: '1004——1',
}, {
value: 'fankui——111',
label: '反馈',
price: '1004——2',
}, ]
}, {
value: 'fankui',
label: 'hhhhh',
price: '1003——2',
}, {
value: 'xiaolv',
label: '效率',
price: '1003——3',
}, {
value: 'kekong',
label: '可控',
price: '1003——4',
}]
}, ]
}, {
value: 'zhinan222222',
label: '指南222222',
price: '200',
}];
var ids = ['kekong', 'xiaolv','zhinan222222'];
var id = 'kekong';
// options.map((optVal,index)=>{
// // console.log(optVal,index)
// if(optVal.value==id){
// console.log(optVal)
// }else{
// }
// })
var idVal
getValData(options);
function getValData(data) {
if (data) {
data.map((optVal, index) => {
if (optVal.value == id) {
idVal=optVal;
console.log(idVal,optVal)
} else {
getValData(optVal.children)
}
})
} else {
return;
}
}
console.log(idVal);
// --------------------
// ids.map((idsVal,idsIndex)=>{
// console.log(ids)
// })
</script>
</body>
</html>