猿问

请各位大佬指点!如何根据某字段分级普通数组,并重组成复合数组?感激不尽

现在有一个包含了多级网点的数组,想根据这个数组中的网点等级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;iif(this.list[i].level==0){//获取1级网点数据
this.listBox[i].children='';
}elseif(this.list[i].level==1){//获取2级网点数据
this.listBox[i].children[i].maxList=this.list[i];
this.listBox[i].children[i].mediumList='',
this.listBox[i].children[i].minList=''
}elseif(this.list[i].level==2){//获取3级网点数据
this.listBox[i].children[i].mediumList=this.list[i];
this.listBox[i].children[i].minList=''
}else{//获取4级网点数据
this.listBox[i].children[i].minList=this.list[i];
}
this.listBox[i].id=this.list[i].id;
this.listBox[i].name=this.list[i].name;
this.listBox[i].lastModified=this.list[i].lastModified;
this.listBox[i].userCount=this.list[i].userCount;
this.listBox[i].deviceCount=this.list[i].deviceCount;
}
console.log(this.listBox);
然而我的方法并没有执行到console.log这一步就凉凉了。而且我这方法效率不高,每次加载页面都要在for循环里遍历所有数据。希望各路大神们赐教一个高效可行的方法,感激不尽!
富国沪深
浏览 352回答 2
2回答

万千封印

这种一般来说会有两个id,一个是自己的id,一个是父级的id。这样来合并。你现在这个没啥意思啊,不适合你那种结构。适合如下结构,按level,push到不同的数组就完事了。{0:[{}],1:[{},{}],2:[{},{}],3:[{},{}],}然后可以遍历上面那个结构,把1加到0[0]的matList上。。。。

慕田峪7331174

根据上楼所说补充:level=0,有几个?如果只有一个就可以把筛选的level=1push进去。level=1,有多个?那根据那个字段吧level=2的数据对应到level=1的数据中。它不适合你想要的结构
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答