3

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

5
formData.objectiveDetails.splice(5, 1)

First argument is the array index and the second the number of items to remove starting from that index.

Sign up to request clarification or add additional context in comments.

Comments

5

You can use Splice to remove the object from the array. Something like this:-

formData.objectiveDetails.splice(5, 1)

1 Comment

Okay that looks good. So I guess underscore isn't really needed here. If I was using underscore would there be an equivalent?
1

Using underscore.js

objectiveDetails = _.without(objectiveDetails, _.findWhere(arr, {id: 5}));

2 Comments

Thanks very much. Actually it looks a bit more complicated than not using _underscore. If you were doing this would you use splice or underscore?
Well, if you just needed it for this particular operation, I would go with splice. But if you needed more specific function helpers, then I'd add underscore.js

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.