I'm trying to delete item from array based on conditional.
var Stack = {};
Stack.items = [];
Stack.items.push({
id: '123',
name: 'Lorem ipsum dolor sit.'
});
Stack.items.push({
id: '154',
name: 'Lorem ipsum dolor sit.'
});
Stack.items.push({
id: '123',
name: 'Lorem ipsum dolor sit.'
});
Now, I'll delete all items with 154 id.
$.each(Stack.items, function(index, item) {
// console.log( item.id );
// console.log( index);
if( item.id === '154' ) {
Stack.items.splice(index, 1);
}
});
But I get an undefined error in the second iteration. I think this is because splice() function modified the original array so the indexes were altered
Uncaught TypeError: Cannot read property 'id' of undefined
I'm getting the expected results with lodash but i'm trying to not use them just for this issue
_.remove(Stack.items, function(item) {
return item.id === '123';
});
Thanks!!