es6之数组去重(对象去重)

2019-03-26  本文已影响0人  匆匆皆过客_294c

一般的数组去重
let a=[1,2,3,4]
let b=[3,4,5]
let union=[...new Set([...a,...b])]

数组中的对象去重
let jsonArray=[{id:'1',result:'xx'},{id:'2',result:'x'}]
let b=[{id:'1',result:'xx'},{id:'3',result:'ll'}]
let c=[...jsonArray,...b]
let d=[]
**关键点
let hash={}
d=c.reduce((item,next)=>{
hash[next.id]?'':hash[next.id]=true&&item.push(next)
return item
},[])

上一篇下一篇

猜你喜欢

热点阅读