Is there any way to remove a specific item from an array. Say for instance, my array is:
Code:
myArray = new Array("item1",item2",item3",item4")
and I want to remove "item2". I won't necessarily know the index, so can you use the CONTENTS of the index to remove it, e.g. say myArray remove "item1"
Failing that, can you query the array about where a particular string is e.g. say, what index contains "item1" - and then remove that index?
You could search through the array for the content to get the index, and then splice() it out . If you don't have to have the items in a particular order, consider using an Object (as an associative array) which will allow you to set and delete content immediately (you will also be able to iterate over it but in an unpredictable order).
Or, if you still need to keep an array, you could keep an Object with the array that would map the content to the array index for fast deletion. The bad part is that you have to update this object when any array operation (e.g. shift()) is made...
Come to think of it, the Array object itself could be used to double the information in the Array as an associative array. It would be cool to create an extention of the Array class that would do this (the array entries would have to have unique .toString() values [too bad there is no hashCode() method ]). Prolly someone has done this already...
It would make such array operations as shift(), splice(), etc. twice as expensive - but deletion would be very cheap...
Thanks for the ideas. The object idea looks like it could be the go. Are you able to point me in the direction of some stuff that I could use - e.g. something in the flash help to get me started.
Mm... donno about the the links, but the using the Object is pretty straightforward:
ActionScript Code:
var assocArray:Object = newObject();
assocArray["item1"] = ""; // you get the added benefit of attaching whatever other info you want here ("" would be the most economical in terms of space I guess)
assocArray["item2"] = [1, 2, 3]; // for example an array could be attachedfor(var i in assocArray){trace("index " + i + " contains " + assocArray[i]);
}