json转换为tree对象(递归)
1 const newTeam = function (data, parentId) { 2 let itemArr = []; 3 for (let i = 0; i < data.length; i++) { 4 let node = data[i]; 5 if (node.parent_id === parentId) { 6 let newNode = {}; 7 newNode.id = node.id; 8 newNode.name = node.name; 9 newNode.parent_id = node.parent_id; 10 newNode.children = newTeam(data, node.id); 11 itemArr.push(newNode); 12 } 13 } 14 return itemArr; 15 } 16 //teamDate 后台传过来的json ,null 第一级的parent_id为null 17 console.log(newTeam(teamDate, null));
这个人比较懒,笔记也粗糙。没得救了。