I have been using the following code:
formData.objectiveDetails.push(emptyObjectiveDetail);
This pushes a new emptyObjectiveDetail object onto the end of an array called objectiveDetails.
If for example the array of objectiveDetails contains 13 objects then how could I remove the one at position 5? I assume I could make this null but what I want to do is to completely remove it so the length of the array becomes 12.
This might be off topic but I have been considering adding underscore.js. Is this something that could be done with underscore?
3 Answers 3
formData.objectiveDetails.splice(5, 1)
First argument is the array index and the second the number of items to remove starting from that index.
Comments
You can use Splice to remove the object from the array. Something like this:-
formData.objectiveDetails.splice(5, 1)
1 Comment
Using underscore.js
objectiveDetails = _.without(objectiveDetails, _.findWhere(arr, {id: 5}));