现在有一个包含了多级网点的数组,想根据这个数组中的网点等级level字段进行数组分级重组,目标是重组成一个复合数组。原数组代码如下:list:[{"id":30064771072,"lastModified":"2019-02-1320:14:42:000","userCount":4,"deviceCount":4,"level":0,"name":"测试网点11"},{"id":30081548288,"lastModified":"2019-01-3014:36:33:729","userCount":0,"deviceCount":0,"level":1,"name":"不过去"},{"id":30081679360,"lastModified":"2019-01-3014:23:33:345","userCount":0,"deviceCount":0,"level":2,"name":"120的子网点"},{"id":30081744896,"lastModified":"2019-01-3006:52:07:915","userCount":0,"deviceCount":0,"level":2,"name":"360"},{"id":30098325504,"lastModified":"2019-01-2909:52:27:640","userCount":0,"deviceCount":0,"level":1,"name":"120"},{"id":30131879936,"lastModified":"2019-01-2910:14:03:897","userCount":0,"deviceCount":0,"level":1,"name":"规划"},{"id":30148657152,"lastModified":"2019-01-3002:44:48:868","userCount":0,"deviceCount":0,"level":1,"name":"小唐"},{"id":30165434368,"lastModified":"2019-01-3014:39:28:452","userCount":0,"deviceCount":0,"level":1,"name":"任务少"},{"id":30182211584,"lastModified":"2019-01-3015:32:40:672","userCount":0,"deviceCount":0,"level":1,"name":"氧化铝"},{"id":30215766016,"lastModified":"2019-01-3015:33:17:230","userCount":0,"deviceCount":0,"level":1,"name":"不过呢"},{"id":30232543232,"lastModified":"2019-01-3007:35:41:689","userCount":0,"deviceCount":0,"level":1,"name":"77"},{"id":30249320448,"lastModified":"2019-01-3008:30:30:981","userCount":0,"deviceCount":0,"level":1,"name":"33344"},],level是网点等级字段,有0,1,2,3这四级。目标数组就是根据这个分级目标数组代码如下:listBox:[{//存放所有数组id:'',name:'',lastModified:'',userCount:'',deviceCount:'',children:[{//存放一级网点数组id:'',name:'',lastModified:'',userCount:'',deviceCount:'',maxList:[{//存放二级网点数组id:'',name:'',lastModified:'',userCount:'',deviceCount:'',minList:[]//存放三级网点数组}]}]}],我本来打算用for循环嵌套if实现,实现的时候进行了多次修改最终实现代码如下:for(leti=0;i
2 回答
万千封印
TA贡献1891条经验 获得超3个赞
这种一般来说会有两个id,一个是自己的id,一个是父级的id。这样来合并。你现在这个没啥意思啊,不适合你那种结构。适合如下结构,按level,push到不同的数组就完事了。{0:[{}],1:[{},{}],2:[{},{}],3:[{},{}],}然后可以遍历上面那个结构,把1加到0[0]的matList上。。。。
慕田峪7331174
TA贡献1828条经验 获得超13个赞
根据上楼所说补充:level=0,有几个?如果只有一个就可以把筛选的level=1push进去。level=1,有多个?那根据那个字段吧level=2的数据对应到level=1的数据中。它不适合你想要的结构
添加回答
举报
0/150
提交
取消