一般数组去重,
let arr = [1,2,3,2,2,3,5,4,4,4]
let newArr = Array.from(new Set(arr))
console.log(newArr)//[1,2,3,5,4]
对象数组去重
const arr = [{
key: '01',
value: '西施'
}, {
key: '02',
value: '王昭君'
}, {
key: '03',
value: '杨玉环'
}, {
key: '04',
value: '貂蝉'
}, {
key: '01',
value: '西施'
}, {
key: '01',
value: '西施'
}];
let curObj = {}
let newArr = arr.filter((item)=>{
if(!curObj[item.key]){
curObj[item.key] = true
return item
}
})
console.log(newArr)
本文介绍了一般数组及对象数组的去重方法。对于一般数组,使用Set结合Array.from实现;对象数组通过filter方法配合对象记录的方式去重。这些技巧有助于提高代码效率。
3153

被折叠的 条评论
为什么被折叠?



