Click to See Complete Forum and Search --> : Array Problems
Hi,
How would I go about removing an element from an array. The element to be removed can be at any index, so I cant use pop().
Any ideas??
Thanks, Lizo.
Khalid Ali
07-26-2003, 09:29 AM
I don't think you want to use pop,because it will remove the last element from the array.
You want to use
array.splice(indexOfElmentToBeRemoved,1);
the above example will remove the element at the provided array index and will return the removed element.
to see more description read the documentation at devedge.netscape.net
AdamGundry
07-26-2003, 10:19 AM
I don't know about why, but I've had some problems using splice(). I forget the details, but in one of the scripts I used I used this instead:
function RemoveElementFromArray(element, array){
result = new Array();
for (i=0;i<array.length;i++){
if (i != element) result[result.length++] = array[i];
}
return result;
}Do you know if any major browsers do not support splice()? Or am I simply wasting my time (as usual)?
Adam
Thanks Khalid, Ive got it working now, you did it again hehe.
Adam, my code looks like this
function deleteColor(i)
{
colors.splice(i,1);
colorNames.splice(i,1);
colorHEXValues.splice(i,1);
colorRGBValues.splice(i,1);
}
and the values I pass to it are taken from the selections made in a select box. Im not sure if this helps you, just thought Id show you how I did it.
Right Ive uploaded everything if you wanna take a look at what Ive been working towards and asking all the questions about. I hope its useful and any feedback would be nice.
http://www.colormixer.tk