import { Injectable } from '@angular/core'; @Injectable() export class TreeService { toTree(olddata){ let newdata = [] function getparentNode(parentId){ return olddata.find((item)=>{ return item.id == parentId }) } olddata.forEach(item => { item.key = item.id item.title = item.name var parentNode = getparentNode(item.parentId); if(parentNode){ if(!parentNode.children){ parentNode.children = [] } if (parentNode.children.length == 0) { item.isTop = true; } else { item.isTop = false; parentNode.children[parentNode.children.length -1].isBottom = false; } item.isBottom = true; parentNode.children.push(item) }else{ if(!item.parentId){//如果parentId为null newdata.push(item) } } }); return newdata; } }