在使用递归时,出现了一些问题。
正常走完循环赋值正确后,会莫名其妙回到for这行代码,xdata,ydata会再次变为null,直接走到return
recursion(drilldownList,name){ var xdata = null var ydata = null for(var i = 0;i < drilldownList.length;i++){ if(name == drilldownList[i].name){ xdata = drilldownList.map((item)=>item.name), ydata = drilldownList.map((item)=>item.value) break }else{ if(drilldownList[i].children){ this.recursion(drilldownList[i].children,name) } } } return [xdata,ydata] },
修改后的,定义变量放在了循环里面,符合条件就return ,不符合条件进入新的循环结果赋值给变量return变量
recursion(drilldownList,name){ let _this=this for(var i = 0;i < drilldownList.length;i++){ var xdata = null var ydata = null if(name == drilldownList[i].name){ xdata = drilldownList.map((item)=>item.name), ydata = drilldownList.map((item)=>item.value) return [xdata,ydata] }else{ if(drilldownList[i].children){ let res = _this.recursion(drilldownList[i].children,name) if(res){ return res } } } } }