I'm trying to loop over some data that look like this:
[0]['fields']['status']['name'] = 'In progress'
[1]['fields']['status']['name'] = 'In progress'
[2]['fields']['status']['name'] = 'In review'
[3]['fields']['status']['name'] = 'In progress'
[4]['fields']['status']['name'] = 'In review'
I'm using the following foreach loop to splice all useless indexes, in this case all of them.
issues.forEach(function (item, index) {
if (issues[index]['fields']['status']['name'] !== "Done") {
issues.splice(index, 1);
}
});
If I loop over the array later I can output 'In progress' and 'In review' which is weird because they should be unset. I think this happens because I manipulate the array while using it. Could someone explain what's wrong and how this can be avoided.
Just loop from the end with an index.
This prevents unseen indices and keeps the index where it belongs to.
var index = issues.length;
while (index--) {
if (issues[index].fields.status.name !== "Done") {
issues.splice(index, 1);
}
}