数组去重小记

1、简单类型数组去重

let numberarray = [1,2,3,4,4,4,66,66,7]
console.log(Array.from(new Set(numberarray)));

2、对象数组去重

let arr = [
        {
    
    id: 0,name:'张三'},
        {
    
    id: 1,name: '李四'},
        {
    
    id: 2,name: '张三'},
        {
    
    id: 3,name: '王五'}
    ]
    // 去重方法---根据指定的属性去重
    function arrDistinctByProp(arr, prop) {
    
    
        return arr.filter(function (item, index, self) {
    
    
            return self.findIndex(el => el[prop] == item[prop]) === index
        })
    }
    //通过name属性去重
    let newArr = arrDeweighting(arr, 'name')
    console.log(newArr)

猜你喜欢

转载自blog.csdn.net/weixin_42744724/article/details/129337199