0

Consider:

var a = [1,2,3];
delete a[1];
// a  = [1, undefined, 3]

Is it possible to delete 2 such that a becomes [1,3]? So that element 1 becomes 3, element 2 disappears and the length is decreased by one.

Adam Zerner
  • 17,797
  • 15
  • 90
  • 156
  • 4
    Yes, you want `splice`. – elclanrs Aug 22 '14 at 19:49
  • Not quite a duplicate, but addressed within [How do I remove an object from an array with JavaScript?](http://stackoverflow.com/questions/3396088/how-do-i-remove-an-object-from-an-array-with-javascript/3396127#3396127) – apsillers Aug 22 '14 at 19:54

1 Answers1

1

Use Array.splice:

> var x = [1, 2, 3, 4];
> x.splice(2, 1);
> console.log(x);
[1, 2, 4]
Mark Ignacio
  • 421
  • 3
  • 8