EN
JavaScript - find difference between two arrays
0
points
In this article, we would like to show you how to find the difference between two arrays using JavaScript.
Practical examples
Example 1
// ONLINE-RUNNER:browser;
const array1 = ['a', 'b', 'c', 'd'];
const array2 = ['a', 'b'];
const result = array1.filter((x) => !array2.includes(x)); // elements that are in array1 but not in array2
console.log(result); // [ 'c', 'd' ]
Example 2
// ONLINE-RUNNER:browser;
const array1 = ['a', 'b'];
const array2 = ['a', 'b', 'c', 'd'];
const result = array2.filter((x) => !array1.includes(x)); // elements that are in array2 but not in array1
console.log(result); // [ 'c', 'd' ]