Well, you could do it an alternate way.
You could add a generic function to the Array object, for deleting items correctly.. i.e. deleting an item and redistributing the array by moving the adjacent items down a step.
Something like:
Array.prototype.deleteItem=function(item){
for(var i=0,n=0;i<this.length;i++){
if(this[i]!=this[item]) this[n++]=this[i]
}
this.length=this.length-1
}
/////////////////////////////////
var a=[1,2,3,4,5]
alert(a +" "+a.length)
a.deleteItem(2)
alert(a +" "+a.length)
Some extra work is needed for accociative arrays.. but this is a great way of extending Array functionality, and keeping functions dealing with "lists" and "stacks" general.
/ Bart
-----Ursprungligt meddelande-----
Från: Scott Andrew LePera <sc...@sc...>
Till: dyn...@li... <dyn...@li...>
Datum: den 14 december 2000 20:24
Ämne: Re: [Dynapi-Dev] deleteFromArray
>> hmm, so a delete array[element] will leave a gap instead of removing the
>> element and move all "higher" elements down one step ?
>
>Yes, Brandon is correct in this. That's one of the reasons why
>removeFromArray was created in the first place. It keeps ordinal arrays
>in the correct order when you remove an element.
>
>--
>scott andrew lepera
>-----------------------------------
>web stuff: www.scottandrew.com
>music stuff: www.walkingbirds.com
>_______________________________________________
>Dynapi-Dev mailing list
>Dyn...@li...
>http://lists.sourceforge.net/mailman/listinfo/dynapi-dev
|