数组数据添加parentId,parentIds,isChild
作者:
瓩千瓦 | 来源:发表于
2020-01-02 14:59 被阅读0次
给排序过的数据添加parentId,parentIds,isChild字段
function add_parentIds(test){![微信图片_20200212173029.jpg](https://img.haomeiwen.com/i16566662/5819308b088285fd.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
let num = '';![微信图片_20200217221854.png](https://img.haomeiwen.com/i16566662/76d8586bc7c7ce76.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
for(let i=0;i<test.length;i++){
let j = i + 1;
if(j <= test.length - 1){
if(test[j]['level'] > test[i]['level']){
test[i]['isChild'] = true;
test[j]['parentId'] = test[i]['id'];
num += test[i]['id'] + ',';
test[j]['parentIds'] = num;
}else{
test[i]['isChild'] = false;
num = '';
}
}
}
return test;
}
```![微信图片_20200212173029.jpg](https://img.haomeiwen.com/i16566662/483d54a66a6a7c98.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
本文标题:数组数据添加parentId,parentIds,isChild
本文链接:https://www.haomeiwen.com/subject/pikooctx.html
网友评论