Open yyman001 opened 5 years ago
https://blog.csdn.net/Mr_JavaScript/article/details/82817177
function setTreeData(source){ let cloneData = JSON.parse(JSON.stringify(source)) // 对源数据深度克隆 return cloneData.filter(father=>{ // 循环所有项,并添加children属性 let branchArr = cloneData.filter(child=> father.id == child.parentId); // 返回每一项的子级数组 branchArr.length>0 ? father.children=branchArr : '' //给父级添加一个children属性,并赋值 return father.parentId==0; //返回第一层 }); } function treeData(source, id, parentId, children){ let cloneData = JSON.parse(JSON.stringify(source)) return cloneData.filter(father=>{ let branchArr = cloneData.filter(child => father[id] == child[parentId]); branchArr.length>0 ? father[children] = branchArr : '' return father[parentId] == 0 // 如果第一层不是parentId=0,请自行修改 }) } // 调用时,字段名以字符串的形式传参,如treeData(source, 'id', 'parentId', 'children')
https://blog.csdn.net/qq_37746973/article/details/78662177 https://github.com/aximario/json-tree/blob/master/src/index.ts
https://blog.csdn.net/Mr_JavaScript/article/details/82817177
https://blog.csdn.net/qq_37746973/article/details/78662177 https://github.com/aximario/json-tree/blob/master/src/index.ts