Javascript Array

2021-09-24  本文已影响0人  殷川

数组常用方法

面试题

求两个数组的交集和差集
交集
const arr1 = [1, 3, 6, 8, 9]
const arr2 = [9, 3]
const result = arr1.filter((v) => arr2.includes(v))
console.log(result) // [3,9]
差集
const arr1 = [1, 3, 6, 8, 9]
const arr2 = [9, 3]
const result2 = arr1.concat(arr2).filter((v) => !arr1.includes(v) || !arr2.includes(v))
console.log(result) // [1,6,8]
上一篇下一篇

猜你喜欢

热点阅读