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)

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章