function list2treeCode(list) {
let set = new Set(list.map(item => item.code.length));
let setOrder = Array.from(set).sort();
let arr = list.filter(item => item.code.length == setOrder[0]);
arr.forEach(item => {
recursion(item, list, setOrder, 1)
})
return arr;
}
function recursion(obj, list, setOrder, curIndex) {
let arr = list.filter(item => item.code.indexOf(obj.code) == 0 && item.code.length == setOrder[curIndex]);
if (arr.length)
obj.children = arr
arr.forEach(element => {
recursion(element, list, setOrder, curIndex + 1)
});
return arr
}
export default list2treeCode
标签:编码,arr,code,树状,list,生成,setOrder,item,let
From: https://www.cnblogs.com/9zhe/p/17139047.html