js递归组织树状结构函数方法
function recursionDataTree(dataList,pid){
let resultList = [];
if (!dataList) return null;
for (const map of dataList) {
let bmid_new = map["id"];
let parentId = map["pid"];
if (pid==parentId) {
const data = map;
let childrenList = this.recursionDataTree(dataList, bmid_new);
if (childrenList)
data["children"]= childrenList;
resultList.push(data);
}
}
return resultList;
}
eg:
let dltest=[{id:'1',pid:'0',name:'大类1'},{id:'2',pid:'0',name:'大类2'},{id:'3',pid:'0',name:'大类3'},{id:'4',pid:'1',name:"中类11"},{id:'5',pid:'1',name:"中类12"},{id:'6',pid:'4',name:"小类111"}];
let resultdata=recursionDataTree(dltest,'0');
结构如下:
[{"id":"1","pid":"0","name":"大类1","children":[{"id":"4","pid":"1","name":"中类11","children":[{"id":"6","pid":"4","name":"小类111","children":[]}]},{"id":"5","pid":"1","name":"中类12","children":[]}]},{"id":"2","pid":"0","name":"大类2","children":[]},{"id":"3","pid":"0","name":"大类3","children":[]}]
版权声明:本文为qq_35997793原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。