js判断两个数组,找出第一个数组中不存在于第二个数组中的值
就是只要获取第一个数组里面有而第二个数组里面没有的
比如[1,2,3]和[3,4,5,6]
只要获取数组二中没有的1,2
js判断两个数组,找出第一个数组中不存在于第二个数组中的值
就是只要获取第一个数组里面有而第二个数组里面没有的
比如[1,2,3]和[3,4,5,6]
只要获取数组二中没有的1,2
let a = [1,2,3]
let b = [3,4,5,6]
a.reduce((arr, item) => {
!b.includes(item) && arr.push(item)
return arr
}, [])
var arr = [1, 2, 3, 4, 5, 6]
var arr2 = [3, 4, 5]
var result = []
for (let i = 0; i < arr.length; i++) {
if (arr2.indexOf(arr[i]) == -1) {
result.push(arr[i])
console.log('arr[i]', arr[i]);
}
}
console.log('result', result);
let arrA = [1,2,3]
let arrB = [3,4,5,6]
let result = []
arrA.forEach(item => {
if(arrA.includes(item)&&!arrB.includes(item)) result.push(item)
})
console.log(result)//[1,2]
这个答案最直接,希望采纳.
const a=[1,2,3]
const b=[3,4,5,6]
const fn=(a,b)=>a.filter(item=>!b.includes(item))
console.log(fn(a,b));
通过 lodash 比较简单
_.difference([1, 2, 3], [3, 4, 5, 6]); // => [1, 2]
具体实现是可以看源码
https://github.com/lodash/lod...
13 回答12.9k 阅读
7 回答2.1k 阅读
3 回答1.3k 阅读✓ 已解决
2 回答1.3k 阅读✓ 已解决
6 回答1.2k 阅读✓ 已解决
6 回答1.1k 阅读
3 回答1.3k 阅读✓ 已解决
const a = [1, 2, 3];
const b = [3, 4, 5, 6];
const map = {};
b.forEach(n => map[n] = true);
const result = a.filter(n => !map[n])