跳到主要内容

reduce

数组对象去重

examples

reduceArray([{ id: 1 }, { id: 2 }, { id: 1 }, { id: 3 }], 'id'); // [{ id: 1 }, { id: 2 }, { id: 3 }]

source code

const reduceArray = (repeatList = [], key = 'id') => {
let obj = {};

return repeatList.reduce((acc, cur) => {
if (obj[cur[key]] ? '' : (obj[cur[key]] = true)) acc.push(cur);
return acc;
}, []);
};