递归的使用
场景:tree菜单重复嵌套
举例:
let newArr= [] arr.forEach(item=>{ newArr.push(this.getElementList(item,{})) })
递归函数:
getElementList(oldObj,newObj) {// newObj是全局参数,用于搜集所有内部参数,也可以设置为数组
if (oldObj.children.length >= 1) {// 内层有子集
oldObj.children.forEach((item)=>{
this.getElementList(item,newObj);
})
}else{// 内层无子集:直接取值
if(oldObj.list.length>0){
oldObj.list.forEach(v=>{
newObj[v.element.data.dispatch_type] = v.element.data.config
})
}
}
return newObj
},
【嵌套数组根据某个字段重新排序】:
listOrder() { function compare(list,key){ list.sort((a,b)=> { const value1 = a[key]; const value2 = b[key]; return value1 - value2; }) list.forEach(item=>{ if (item.children && item.children.length > 0) { item.children = compare(item.children,key) } }) return list } return compare(this.list,'order') },

浙公网安备 33010602011771号