過濾對象數組中key值相同的數據

如果有遇到一個數組中存儲多個對象,有部分對象的某一個鍵值是相同的,需要過濾掉重複項的實現方案

const arr =[
  {
    "count": 1095,
    "month": "三月",
  },
  {
    "count": 1106,
    "month": "三月",
  },
  {
    "count": 987,
    "month": "四月",
  },
  {
    "count": 1166,
    "month": "四月",
  },
  {
    "count": 753,
    "month": "五月",
  },
  {
    "count": 891,
    "month": "五月",
  }
];
const res = new Map();
const new2 = arr.filter(item => !res.has(item.month) && res.set(item.month, 1));
console.log(arr.length, new2);
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章