Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I have an object:

data.Dealer.car[0]
               [1]
               [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 from the alert, even though the actual array element now doesn't exist?

Any ideas?

share|improve this question
    
possible duplicate of JavaScript Array Delete Elements – Jarrod Roberson Apr 30 '15 at 17:05
up vote 14 down vote accepted

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.

share|improve this answer
    
Yeah, and the element 2 will be undefined . – e-satis Jan 30 '09 at 15:26
    
What happens when you delete something at the last index in the array? – Outlaw Programmer 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
    
Yes, JavaScript arrays can, in fact, be sparse. – Ryan O'Hara Sep 13 '13 at 3:44

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.

share|improve this answer

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

share|improve this answer

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()

share|improve this answer
    
That's not what sparse means. – Dara Javaherian Apr 12 '13 at 15:47
2  
@DaraJavaherian: That is what sparse means. (No? At least say what your definition of it is…) – Ryan O'Hara Apr 28 '13 at 17:07

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

share|improve this answer

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;
share|improve this answer
2  
You want to remove the last item? Just use: data.Dealer.car.pop(); – some Jan 30 '09 at 22:05

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.