请问怎么写?
发布于 6 年前 作者 XuQin15 4612 次预览 最后一次回复是 6 年前 来自 问答
1.有一个摊平的数组描叙了一系列的地理信息,类似于: var locationList=[ { id: 0, name : ”中国” } { id: 1, pid: 0 , name: ”广东省” } { id: 2 , pid:1 , name: ”深圳市” } { id: 3 , pid:2 , name: ”广州市” } … ] 其中每个节点的’pid’指向了他所属上级地区。现在要求你把这个数组输出成树状结构 var locationTree = buildLocationTree(locationList); console.log(locationTree); 其中’locationTree’的结构应该如下: interface LocationTree{ root:LocationNode; } interface LocationNode{ id: number; pid?: number: name: string; subLocations?: LocationNode[]; } 请实现’buildLocationTree()’,输出的父节点里面包含的子节点的数组
2 回复
顺便说一下,返回 {‘root’: xxx} 这种并不是很好,如果不止一个root就不好弄了。 建议返回为数组,比如这样 [{ …, sub:[] }, {…, sub:[] }]