Reputation: 1771
How do I delete an item from an array without null as the result?
var a = [1,2,3];
delete a[1];
a;
result: [1,null,3];
desired result: [1,3];
Upvotes: 1
Views: 73
Reputation: 91608
The delete
operator simply removes a property from an object, so that property becomes null. It won't resize your array automatically.
To do this, use the Splice
method:
var a = [1,2,3];
a = a.splice(1, 1); // Remove one element from array, starting at index 1
More Info on JavaScript Splice Method
Upvotes: 2