亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何首先組合對象的屬性,然后使用 Javascript 刪除對象數組中的重復項

如何首先組合對象的屬性,然后使用 Javascript 刪除對象數組中的重復項

HUWWW 2022-12-22 13:46:45
我這里有一組對象:const arr = [  { id: 1, name: "test1", quantity:1 },  { id: 2, name: "test2", quantity:1 },  { id: 2, name: "test3", quantity:1 },  { id: 3, name: "test4", quantity:1 },  { id: 4, name: "test5", quantity:1 },  { id: 5, name: "test6", quantity:1 },  { id: 5, name: "test7", quantity:1 },  { id: 6, name: "test8", quantity:1 }];我想在刪除它們之前將大量重復對象加在一起所以結果是:const arr = [  { id: 1, name: "test1", quantity:1 },  { id: 2, name: "test3", quantity:2 },  { id: 3, name: "test4", quantity:1 },  { id: 4, name: "test5", quantity:1 },  { id: 5, name: "test6", quantity:2 },  { id: 6, name: "test8", quantity:1 }];我已經看到它的變體使用 map 或 reduce 刪除重復項,但我還沒有看到任何我想在不使用太多循環的情況下以雄辯的方式完成的事情。我一直在思考如何最好地完成這一天,但沒有找到任何幫助,我們將不勝感激
查看完整描述

3 回答

?
蠱毒傳說

TA貢獻1895條經驗 獲得超3個贊

您可以將 reduce 與對象一起使用,以存儲具有每個 id 的元素。


const arr = [

  { id: 1, name: "test1", quantity:1 },

  { id: 2, name: "test2", quantity:1 },

  { id: 2, name: "test3", quantity:1 },

  { id: 3, name: "test4", quantity:1 },

  { id: 4, name: "test5", quantity:1 },

  { id: 5, name: "test6", quantity:1 },

  { id: 5, name: "test7", quantity:1 },

  { id: 6, name: "test8", quantity:1 }

];

const res = Object.values(

  arr.reduce((acc,curr)=>{

  acc[curr.id] = acc[curr.id] || {...curr, quantity: 0};

  acc[curr.id].quantity += curr.quantity;

  return acc;

  }, {})

);

console.log(res);


查看完整回答
反對 回復 2022-12-22
?
MM們

TA貢獻1886條經驗 獲得超2個贊

const arr = [

    { id: 1, name: "test1", quantity: 1 },

    { id: 2, name: "test2", quantity: 1 },

    { id: 2, name: "test3", quantity: 1 },

    { id: 3, name: "test4", quantity: 1 },

    { id: 4, name: "test5", quantity: 1 },

    { id: 5, name: "test6", quantity: 1 },

    { id: 5, name: "test7", quantity: 1 },

    { id: 6, name: "test8", quantity: 1 }

];


var result = arr.reduce(function (r, a) {

    r[a.id] = r[a.id] || { id: a.id, quantity: 0, name: a.name };

    r[a.id].quantity += a.quantity;

    return r;

}, Object.create(null));


console.log(JSON.stringify(result));


查看完整回答
反對 回復 2022-12-22
?
有只小跳蛙

TA貢獻1824條經驗 獲得超8個贊

使用forEach具有聚合數量計數的循環和構建對象。


const convert = (arr) => {

  const res = {};

  arr.forEach(({ id, ...rest }) =>

    res[id] ? (res[id].quantity += 1) : (res[id] = { id, ...rest })

  );

  return Object.values(res);

};


const arr = [

  { id: 1, name: "test1", quantity: 1 },

  { id: 2, name: "test2", quantity: 1 },

  { id: 2, name: "test3", quantity: 1 },

  { id: 3, name: "test4", quantity: 1 },

  { id: 4, name: "test5", quantity: 1 },

  { id: 5, name: "test6", quantity: 1 },

  { id: 5, name: "test7", quantity: 1 },

  { id: 6, name: "test8", quantity: 1 },

];


console.log(convert(arr));



查看完整回答
反對 回復 2022-12-22
  • 3 回答
  • 0 關注
  • 114 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號