数组

1.合并数组,其中一项相加(id相同)

let res = []

 let arr = JSON.parse(JSON.stringify(this.child))

   res =  Object.values(arr.reduce((t,c)=>{

       (!t[c.id] && (t[c.id]= c)) || (t[c.id] && (t[c.id].num += c.num));

            return t;

    }, {}));

2.合并数组,找到相同项去重

 const res = new Map();

  let c = this.child.filter((a)=> !res.has(a.id) && res.set(a.id,1))

3.记录数组的个数,以数组一项作为箭值.组成对象

 this.childSel = new Object();

       for(let i in this.child){

        var id = this.child[i].id

          if(this.childSel[id]===undefined){

            this.childSel[id] = 1

          }else{

            this.childSel[id] +=1

          }

你可能感兴趣的:(数组)