数组对象去重(用reduce方法)
数组对象去重
let hash = {}
let config = [{
id: 2,
name: 'Y',
}, {
id: 3,
name: 'A',
}, {
id: 5,
name: 'S',
}]
const newArr = config.reduceRight((item, next) => {
hash[next.id] ? '' : hash[next.id] = true && item.push(next)
return item
}, [])
console.log(newArr) //去重后的新数组,包含去重的元素