12

I have an array:

data.Dealer.car[0]
data.Dealer.car[1]
data.Dealer.car[2]

If I do this:

alert(data.Dealer.car.length);
delete data.Dealer.car[1];
alert(data.Dealer.car.length);

It gives me the same count each time. Does the removed element still exist?

chharvey
  • 8,580
  • 9
  • 56
  • 95
joe90
  • 538
  • 2
  • 5
  • 19
  • possible duplicate of [JavaScript Array Delete Elements](http://stackoverflow.com/questions/500606/javascript-array-delete-elements) –  Apr 30 '15 at 17:05
  • @JarrodRoberson — It doesn’t seem to be an exact duplicate. The question you linked is asking for the differences between `delete` and `Array.prototype.splice`, whereas this question is basically asking why deleting an element from an array (thus making it sparse) won’t reduce its length. – chharvey Mar 03 '18 at 17:18
  • I've edited the question to make it clearer. – chharvey Mar 03 '18 at 17:31

6 Answers6

20

If you want to remove an item, use the splice method:

alert(data.Dealer.car.length);
data.Dealer.car.splice(1, 1);
alert(data.Dealer.car.length);

But notice that the indices have changed.

Gumbo
  • 643,351
  • 109
  • 780
  • 844
14

JavaScript arrays aren't sparse, if you have a 0 and a 2, then element 1 must exist. Meaning the length is going to be 3.

Ray Hidayat
  • 16,055
  • 4
  • 37
  • 43
  • 1
    Yeah, and the element 2 will be undefined . – Bite code Jan 30 '09 at 15:26
  • What happens when you delete something at the last index in the array? – Tim Frey Jan 30 '09 at 15:37
  • 1
    The length will be untouched if you delete the last element. You can set the length to shrink the array to any size you want. – Ates Goral Jan 30 '09 at 16:11
  • 2
    Yes, JavaScript arrays can, in fact, be sparse. – Ry- Sep 13 '13 at 03:44
  • JS arrays *can* be sparse. Sparse arrays are arrays that have one or more uninitialized elements. For example `let arr = ['foo' , , 'bar']` is a sparse array, so it has length `3`, even though `arr[1]` does not have a value. – chharvey Mar 03 '18 at 17:13
5

Building on what @Ray Hidayat posted:

JavaScript arrays are sparse. If you have an array of length 3, deleting the reference at [1] will simply "unset" that item, not remove it from the array, or update that array's length. You could accomplish the same with

myArray = [0, , 2, , , , ];  // length 6; last three items undefined

Here's a Fiddle: http://jsfiddle.net/WYKDz/

NOTE: removing items from the middle of large arrays can be computationally intensive. See this post for more info: Fastest way to delete one entry from the middle of Array()

Community
  • 1
  • 1
zedd45
  • 2,101
  • 1
  • 31
  • 34
5

Array.shift() would remove the first item from the array and make it shorter. Array.pop() will remove the last item.

Jeremy French
  • 11,707
  • 6
  • 46
  • 71
2

I think you're looking for this:

var arr = [0,1,2,3,4];

alert( arr.splice( 2, 1 ) ); // alerts 2, the element you're removing
alert( arr ) // alerts 0,1,3,4  - the remaining elements

here's a MDC reference

meouw
  • 41,754
  • 10
  • 52
  • 69
0

Ok.. fixed this now as the array was still allocated.

I needed to basically do:

var newCar = new Array();
for (i = 0 ; i < tblSize -2; i ++)
{
    newCar[i]=data.Dealer.car[i];
}
data.Dealer.car = newCar;
joe90
  • 538
  • 2
  • 5
  • 19