openks / learn-vue

自定义组件文档
https://openks.github.io/learn-vue
0 stars 0 forks source link

20180425_如何使用javascript处理数组的交集并集及差集 #102

Open openks opened 6 years ago

openks commented 6 years ago

timg

let a=[1,2,3,4,5,6,7,8,9]
let b=[0,2,4,6,8,10]

// 并集 
[...(new Set([...a,...b]))]
//交集
 a.filter(v=>b.includes(v))
//差集(也可以理解为并集-交集) 
a.concat(b).filter(v=>!a.includes(v)||!b.includes(v))