How to find two arrays differences and form differences in new array in JavaScript?

45 Views Asked by At

How do I get differences between 2 arrays and form differences in new array?

arr1 = [1,2,3,4,5];

arr2 = [1,2,3,4];

newArr = [5];

Answer must be the same when arr1 and arr2 switch places.

1

There are 1 best solutions below

0
Consxious On

We can use .includes()/ .indexOf() to resolve this q.

function diffArr(arr1, arr2) {
return arr1  
.concat(arr2)  //Join 2 arr into 1 new array
.filter(item => !arr1.includes(item) || !arr2.includes(item));
} //compare and remove arr1 & 2 with new array