var tradeList = [{
"value": "TRADE_BIGDATA",
"label": "大数据",
"children": [{
"value": "C",
"label": "IaaS"
}, {
"value": "B",
"label": "PaaS"
}, {
"value": "G",
"label": "量化分析"
}]
}, {
"value": "TRADE_MACHINERY",
"label": "机械设备",
"children": [{
"value": "D",
"label": "建筑工程机械"
}, {
"value": "G",
"label": "机床机械"
}, {
"value": "K",
"label": "食品机械"
}]
}, {
"value": "TRADE_FARMING",
"label": "农林渔牧",
"children": [{
"value": "I",
"label": "生产设备"
}, {
"value": "D",
"label": "林业"
}, {
"value": "H",
"label": "农副产品加工"
}, {
"value": "F",
"label": "畜牧业"
}]
}, {
"value": "TRADE_VEHICLE",
"label": "交通工具",
"children": [{
"value": "B",
"label": "船舶厂商"
}, {
"value": "A",
"label": "飞机厂商"
}]
}]
// function flatten(origin) {
// var result = [];
// for(var i = 0; i< origin.length; i++) {
// var item = origin[i];
// if(Array.isArray(item)) {
// result = result.concat(flatten(item))
// } else {
// result.push(item);
// }
// }
// return result;
// }
// var arr = ["a", "b", ["c", "d"], [["d"],"e"], "f"];
// flatten(arr);
// console.log(flatten(arr))
/* 数据大致解构为
*[
* {
* value:'a',
* label:'b',
* children:[{value:.....}]
* }
* ]
*/
// console.log(tradeList)
// key是当前选择的某一项 如 :
/*
* {
* value:'a',
* label:'b',
* children:[{value:.....}]
* }
*/
let key = [
{
value: "TRADE_FARMING"
}
]
function getIndexByArrayKey(list, groupKey){
let arr = list
const data = []
// 声明一个方法用于展开数组
function flatten(origin, index) {
//可以用来接收返回的值,目前没用
const result = []
for (let i = 0; i < origin.length; i++) {
const item = origin[i]
if (item.value === groupKey[index].value) {
arr = item.children
data.push(i)
}
}
return result
}
for (let i = 0; i < groupKey.length; i++) {
flatten(arr, i)
}
return data
}
const idnex = getIndexByArrayKey(tradeList,key)
console.log(idnex,tradeList[idnex].label)
console