functionreturnIt(code){//returnobj}通过已知value(编码)返回对象e.g.returnIt('000003');返回{value:'000003',label:'渭南市',isLeaf:true}constcityList=[{label:'全国',value:'000000',children:[{label:'陕西省',value:'000001',children:[{value:'000002',label:'西安市',isLeaf:true},{value:'000003',label:'渭南市',isLeaf:true},],},{label:'上海',value:'000005',isLeaf:true},{label:'北京',value:'000006',isLeaf:true},{label:'河南省',value:'000007',children:[{value:'000008',label:'信阳',isLeaf:true},{value:'000009',label:'周口',isLeaf:true},{value:'000010',label:'其他',isLeaf:true},],},{label:'山东省',value:'000011',children:[{value:'000012',label:'枣庄',isLeaf:true},{value:'000013',label:'烟台',isLeaf:true},{value:'000014',label:'淄博',isLeaf:true},],},]}];
2 回答
MYYA
TA贡献1868条经验 获得超4个赞
简单递归一下就行了functionreturnIt(code,list){for(letitemoflist){if(item.value===code)returnitem;if(item.children){constchild=returnIt(code,item.children);if(child)returnchild;}}}因为是纯函数,调用需把cityList作为参数传递进去constresult=returnIt('000003',cityList);console.log(result);
qq_笑_17
TA贡献1818条经验 获得超7个赞
functiongetReturnIt(arr,value){letnewarr=arr;letcurr=null;while(newarr.length>0){curr=newarr.shift();if(value===curr.value){returncurr}if(curr.children){newarr.push(...curr.children)}}}console.log(getReturnIt(cityList,'000008'))
添加回答
举报
0/150
提交
取消