目录

​简单数组去重​

​对象数组去重​

简单数组去重

function arrDistinct(arr){
const newArr = new Set(arr);
return [...newArr]
}

使用范例

let list = [1,2,2,3,4,4]
console.log(arrDistinct(list))

对象数组去重

function arrDistinctByProp(arr,prop){
//只返回第一次出现的,重复出现的过滤掉
return arr.filter(function(item,index,self){
return self.findIndex(el=>el[prop]==item[prop])===index
})
}

使用范例

let list = [
{
key:'1',
name:'林青霞'
},
{
key:'2',
name:'张三丰'
},
{
key:'1',
name:'段誉'
},
{
key:'1',
name:'段誉'
}
]

//通过name属性去重
console.log(arrDistinctByProp(list,'name'))