Within my Angular controller I have created a method which is attached to a click handler on my scope. This click handler checks to see whether a checkbox is checked or unchecked and either pulls the associated objects out of an array or puts them back in.
My else block, where I push objects back into the array, works great. However, my if block where I attempt to splice these objects from the array does not work.
Here's the relevant code:
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 believe what is happening is that my array's length shrinks each time I splice from it, so I inevitably run into a Cannot read property 'type' of undefined error halfway through the loop. How could I go about handling removing these objects from the array without running into this issue?
for (var i = length - 1; i >= 0; i--)worked like a charm! If you want to toss that up as an answer I'd love to give you credit for your help.