On my Angular controller, I've set up a method linked to a click handler in the scope. This function checks if a checkbox is checked or unchecked and toggles the associated objects in an array accordingly.
The code block where I push objects back into the array with the else statement works perfectly. However, the if block where I attempt to use splice to remove objects from the array does not work as expected.
Take a look at the relevant code below:
vm.checkToggle = function(isChecked, value) {
if (!isChecked) {
var length = vm.markers.length;
for (var i = 0; i < length; i++) {
if (vm.markers[i].type === value) {
vm.markers.splice(i, 1);
}
}
} else {
...
};
I'm facing an issue where the array length decreases each time I perform a splice operation, resulting in a 'Cannot read property 'type' of undefined' error midway through the loop. How can I handle removing these objects from the array without encountering this problem?