js 数组对象去重合并

let newArr = [],
        arr = [
          { id:1, text: '1', text2: '2' },
          { id:1, text3: '3' },
          { id:2, text1: '4' },
          { id:2, text2: '5' },
          { id:3, text1: '5' },
        ];
      arr.forEach(function(item){
        let hasPush = false;
        newArr.forEach((item2,index,thisArr)=>{
          if(item.id == item2.id){
            hasPush = true;
            thisArr[index] = {...item,...item2};
            return;
          }
        }); 
        !hasPush&&newArr.push(item)
      });
      console.log(newArr)

js 数组对象去重合并_第1张图片

你可能感兴趣的:(js,html)