Categories
discuss

Why doesn’t deleting from a Javascript array change its length?

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 …

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?

Answer

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.

Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..