//id 去重
const arr = [
{ id: 1, name: 'John' },
{ id: 2, name: 'Jane' },
{ id: 1, name: 'John' },
{ id: 3, name: 'Bob' },
];
const result = [...new Set(arr.map(item => JSON.stringify(item)))].map(item => JSON.parse(item));
console.log(result);
// [{ id: 1, name: 'John' }, { id: 2, name: 'Jane' }, { id: 3, name: 'Bob' }]
// name 去重
const arr = [
{ id: 1, name: 'John' },
{ id: 2, name: 'Jane' },
{ id: 4, name: 'John' },
{ id: 3, name: 'Bob' },
]
const result = Object.values(arr.reduce((acc, cur) => {
acc[cur.name] = cur
return acc
}, {}))
console.log(result);
js 数组对象去重的方式
于 2023-03-07 14:53:10 首次发布