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?

Jason Varga
  • 1,949
  • 2
  • 21
  • 29

2 Answers2

3

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

array.splice(1,1);
steven
  • 646
  • 4
  • 8
2

Use the splice method instead:

array.splice(1, 1);

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