Vue array deduplication
Recently encountered the need to deduplicate the array in development, here is the basic deduplication method recorded
Array deduplication
For simple array deduplication, you can use the Set collection, which can basically deduplicate all data types
test() {
const arr = [true, 1, 'str', 's', 2, false, true, {}, [], function() {}, {}, [1], [2], { obj: 1 }, 2, 'str']
const qc = [...new Set(arr)]
console.log('qc')
console.log(qc)
}
console print result
1
2
3
4
5
6
Deduplication of object arrays To deduplication
of object arrays, you can use the Map collection to deduplication through a certain attribute in the object
test() {
const arr = [
{
name: '张三',
age: 22
},
{
name: '李四',
age: 22
},
{
name: '张三',
age: 23
}
]
const map = new Map()
const qc = arr.filter(key => !map.has(key.name) && map.set(key.name, 1)) // 这里对name属性进行去重
console.log('qc')
console.log(qc)
}
Original link: https://blog.csdn.net/q_w_e_rt_y/article/details/115673629