數組/對象數組去重

 

1.對象數組去重javascript

let hash = {}
    let config = [{
            id: 2,
            name: 'Y',
        }, {
            id: 3,
            name: 'A',
        }, {
            id: 5,
            name: 'S',
        }]
const newArr = config.reduceRight((item, next) => {
        hash[next.id] ? '' : hash[next.id] = true && item.push(next)
        return item
    }, [])
console.log(item)

2.字符串數組去重
 1.es6方法java

let array = [1,2,3,4,5]
array = Array.from(new Set(array));
 console.log(array)

 2.其餘方法es6

 

//方法1

let arr = [1,2,3,1,'a',1,'a']

arr.filter(function(ele,index,array){ return index===array.indexOf(ele) })
相關文章
相關標籤/搜索