Reputation: 119
Hello i'm a beginner in nodejs and i just wanted to know how can i compare 2 array and store the difference into an other array.
i've done this to start :
const array1 = [1,2,3,4,5,6,7,8,9,0];
const array2 = [5,2,8,9];
const array3 = []; //wanted [1,3,4,6,7,0]
var i=0
array2.forEach(function(element){
const found = array1.find(element => element !== array2[i])
array3.push(found)
i++
})
console.log(array3)
Thanks a lot !
Upvotes: 0
Views: 39
Reputation: 10204
You can implement that using Array.filter
& Array.includes
function.
const array1 = [1,2,3,4,5,6,7,8,9,0];
const array2 = [5,2,8,9];
const result = array1.filter(item => !array2.includes(item));
console.log(result);
Upvotes: 1
Reputation: 14891
You could use filter
and includes
const array1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
const array2 = [5, 2, 8, 9]
const array3 = array1.filter((element) => !array2.includes(element))
console.log(array3)
Upvotes: 1