1、使用Map
const arr = [{id: 1, name: 'x1'}, {id: 2, name: 'x2' },{id: 1, name: 'x1'}]
console.time('all time')
const map = new Map()
const res = arr.filter(item => !map.has(item.userId) && map.set(item.userId, 1))
console.timeEnd('all time');
2、使用对象key唯一性
const arr = [{id: 1, name: 'x1'}, {id: 2, name: 'x2' },{id: 1, name: 'x1'}]
console.time('all time')
const obj = {}
arr.map(item=>{
obj[item.id] = item
})
const newList = []
for(let key in obj){
newList.push(obj[key])
}
console.timeEnd('all time');