I am looking for a cleaner and more efficient solution in javascript to compare two arrays and create a third one.
So I have two arrays :
var array1 = [
[{
id: 1,
enabled: false
}],
[{
id: 2,
enabled: true,
}],
[{
id: 10,
enabled: false
}]
]
var array2 = [
{
id_from_array1: 10,
data: true
},
{
id_from_array1: 20,
data: false
},
{
id_from_array1: 1,
data: true
}
]
And i want to extract from the second array the IDs that are not present in the first array, so for now my solution is to create a third array with a double loop to compare the values of the first two arrays :
var array3 = [];
for (var i = 0; i < array2.length; i++) {
for (var y = 0; y < array1.length; y++) {
if (array2[i].id_from_array1 === array1[y][0].id) {
array3.push(array2[i])
}
}
}
can we do better ?
Thx!