如何在 JavaScript 中获取两个数组之间的差异?

有以下两个数组

var arr1 = ['a', 'b', 'c'];
var arr2 = ['b', 'c', 'd'];

1
let difference = arr1.filter(x => !arr2.includes(x)); //['a']
2
let intersection = arr1.filter(x => arr2.includes(x)); // ['b', 'c']
3
let difference2 = arr1 .filter(x => !arr2.includes(x)) .concat(arr2.filter(x => !arr1.includes(x))); //['a', 'd']

参考:SO

posted @ 2023-02-15 16:34  strongerPian  阅读(438)  评论(0编辑  收藏  举报
返回顶端