数组数据添加parentId,parentIds,isChild

2020-01-02  本文已影响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)
上一篇下一篇

猜你喜欢

热点阅读