I have these two arrays :
array1 = [{firstname: 'abc', age: 24}];
array2 = [{name: 'bcg', age: 33}, {name:'abc', age: 55}];
I want to loop through both arrays and remove the object from the second array (array 2
) that has a the same name key value as the first object in array 1. So essentially loop through both arrays to see where key values match and then remove the relevant object array2[1]
from the second array.
I tried doing this but it did not work:
for (let p = 0; p < array1.length; p++) {
for (let i = 0; i < array2.length; i++) {
if (array1[p].firstname === array2[i].name) {
array2.splice(i,1);
}
}
}
Is there a way this can work with JavaScript ?