0

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
  • arr1.filter(k=>!arr2.includes(k)) Commented Jul 4, 2020 at 11:24

1 Answer 1

0

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
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.