数组去重-方法一:利用reduce结合find
const newList = list.reduce((acc, cur) => {
const hasItem = acc.find(item => item.id === cur.id)
if (!hasItem) {
acc.push(cur)
}
return acc
}, [])
数组去重-方法二:利用filter结合findIndex
const newList = list.filter(
(item, index, arr) => arr.findIndex(i => i.id === item.id ) === index
)
数组去重-方法三:利用ES6的新方法Set
注意:set只能为简单数据类型组成的数组去重,如果该数组的数据项是复杂类型(例如对象),则会去重失败。
Array.from(new Set(list))