6

In Javascript, I have an array of objects like so:

var array = [{ foo: 'bar' }, { foo: 'baz' }, { foo: 'qux' }]; 

which looks like this, really...

[0: {...}, 1: {...}, 2: {...}] 

and I delete the second one:

delete array[1]; 

then I have this:

[0: {...}, 2: {...}] 

How can I adjust this array so the keys are back in numerical order?

1
  • ^ If you don't use delete, you don't have to worry about it. Commented Mar 19, 2014 at 23:32

2 Answers 2

3

I believe Array.splice is what you are looking for in this case

array.splice(1,1); 
Sign up to request clarification or add additional context in comments.

Comments

2

Use the splice method instead:

array.splice(1, 1); 

Will remove 1 object at index 1, without leaving an empty space.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.