I'm currently attempting to compare two arrays containing multiple objects and remove elements based on a condition. To my surprise, while testing in Chrome's console, I noticed that the first array (arr1) is not being emptied even though I am using an if condition that should always evaluate to true. After the initial iteration, there is always at least one object remaining, and it takes several loops for the element to finally be removed. Is there something obvious that I'm overlooking? It feels like such a fundamental JavaScript mistake.
var car1 = {
type: 'das',
model: '1',
color: 'blue'
} //sample object
arr1 = []
for (i = 0; i < 5; i++) {
arr1[i] = car1
} //filling object with values
for (i in arr1) {
if (arr1[i].type == 'das') { //removing element with condition which always matches
arr1.splice(i, 1);
}
}
console.log(arr1);