Click to See Complete Forum and Search --> : Remove element from array


michelle
03-27-2003, 06:53 AM
Two questions actually:

1: How can I start an array on [1] instead of [0].
element.lenght returns three in this:

var element = new Array()
element[1] = "michelle";
element[2] = "de beer";

2: How can I remove an element totally from the array?
What I do now is to set the array-element to null, but it only replaces the contents of the array with null.

Try the code below to see my point:

var element = new Array()
element[1] = "michelle";
element[2] = "de beer";
element[2] = null;
alert(element.length);
for (var i = 1; i < element.length ; i++) {
alert(element[i]);
}

I want the alert(element.length); to alert 2, not 3...

Any thoughts?
// Michelle

khalidali63
03-27-2003, 08:03 AM
First of all I dont think thee is a straight forward way of doing this if you want to remove an element in the middle of the array.For removing the last element of the array you can use 2 methods

myArray.length = newLength;

will remove all the elements beyond the newlength value.

another way of getting this done is using Array,pop() javascript function,this function removes and returns the last element in array

myArray.pop() will remove the last element.

Hope this helps

Cheers

Khalid

pyro
03-27-2003, 08:04 AM
Change you alert line to this:

alert(element.length-1);