I am facing a challenge with an array of indices and an array of objects:
let indices = [1,3,5]
let objArray = [{name: "John"}, {name: "Jack"}, {name: "Steve"}, {name: "Margot"},
{name: "Tim"}, {name: "Elma"}]
My goal is to update objArray
in a way that the entries matching the indices from the indices array are removed.
For example, the updated objArray
should be:
[{name: "John"}, {name: "Steve"}, {name: "Tim"}]
as elements at positions 2, 4, and 6 matched the indices in the indices array.
I attempted to achieve this, however, the indexOf
method is consistently returning -1.
for(let i=0;i<indices.length;i++) {
if(objArray.indexOf(indices[i]) > -1) {
delete objArray[indices[i]];
}
}