es6之数组去重之对象去重

简单的数组去重:

let a = [1,2,3,4];
let b = [3,4,5];
let newList = [...new Set([...a, ...b])]
//[1, 2, 3, 4, 5]

数组中对象去重:

let a=[{id:'1',value:'this'},{id:'2',value:'is'}]
let b=[{id:'1',value:'hello'},{id:'3',value:'world'}]
let c=[...a,...b]                  
let d=[]
//关键点
let hash={}
d=c.reduce((item,next)=>{
hash[next.id]?'':hash[next.id]=true&&item.push(next)
return item
},[])

es6之数组去重之对象去重_第1张图片

你可能感兴趣的:(es6之数组去重之对象去重)