Seeking a more efficient and streamlined approach in javascript to compare two arrays and generate a third one.
We have the following 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
}
]
The goal is to extract IDs from the second array that are not present in the first array. Currently, this is done by creating a third array with nested loops to compare 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])
}
}
}
Is there a more optimal solution to this problem?
Thank you!