17

I have an array like below:

 var fields = [
    {name:"mark", age:"23"}, 
    {name:"smith", age:"28"}, 
    {name:"kelvin", age:"25"}, 
    {name:"micheal", age:"22"}
];

I understand that fields will now have index/keys 0,1,2,3

How do I delete index 2 and reset keys so that we now have 0,1,2 instead of 0,1,3

2
  • That's actually just a normal array, with objects. Commented Jul 10, 2016 at 20:38
  • 5
    array.splice(2,1); Commented Jul 10, 2016 at 20:38

1 Answer 1

39

If I understand this correctly, you want to remove the array element at index 2 and re-index the array so that there is no empty space. If that's the case, JavaScript has got you covered.

This will modify your original array and will remove only one element starting at index 2.

fields.splice(2,1);

MDN Reference

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

4 Comments

Just a heads up, I meant to upvote, tag me if an edit is made and I'll reverse the downvote
@SpencerMay Just submitted an edit, should likely be shown by the time you check this notification, so you'd be able to retract it then.
@SyedM.Sannan guess it wasn't enough of an edit because it still says locked in :/ sorry
@SpencerMay I got the edit notification just half an hour ago, should work now :)

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.