Click to See Complete Forum and Search --> : delete array object & element


Gord
02-09-2006, 08:16 AM
I have an array which contains objects & am trying to delete one of the objects & the array reference to it.

eg:

var myArray = new Array (object1,object2,object3);

//myArray.length=3 at this point

delete myArray[2];

// myArray.length still = 3; object3 has been deleted.

I want myArray.length to = 2 after the deletion

How can I delete myArray[2] (Just element 2 , not the complete array) so that it is 'Undeclared" rather than "Undefined" and future references to myArray.length will return a value of 2 ?

Thanks Gord

LeeU
02-09-2006, 08:30 AM
You should be able to do it using pop():

[from code_punk]


var dynamic_array = new Array();

function add_bottom(){
var item = document.form1.inputbox.value;
dynamic_array.push(item);
document.form1.outputbox.value = dynamic_array;
}//ends add_bottom function

function remove_bottom(){
dynamic_array.pop();
document.form1.outputbox.value = dynamic_array;
}//ends remove_bottom function


The pop() method removes the last item of the array.

Gord
02-09-2006, 08:41 AM
Excellent . Many thanks