Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 01-29-2006, 09:14 PM   #1
ubergrafik
milky...
 
ubergrafik's Avatar
 
Join Date: Jan 2005
Posts: 370
Send a message via MSN to ubergrafik
Default removing a specific item from an array

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?
ubergrafik is offline   Reply With Quote
Old 01-29-2006, 09:58 PM   #2
astgtciv
Resu Deretsiger
 
astgtciv's Avatar
 
Join Date: Jul 2005
Location: St-Petersburg, Russia
Posts: 2,329
Default

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...
astgtciv is offline   Reply With Quote
Old 01-29-2006, 10:57 PM   #3
astgtciv
Resu Deretsiger
 
astgtciv's Avatar
 
Join Date: Jul 2005
Location: St-Petersburg, Russia
Posts: 2,329
Default

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...
astgtciv is offline   Reply With Quote
Old 01-29-2006, 11:07 PM   #4
ubergrafik
milky...
 
ubergrafik's Avatar
 
Join Date: Jan 2005
Posts: 370
Send a message via MSN to ubergrafik
Default

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.
ubergrafik is offline   Reply With Quote
Old 01-29-2006, 11:15 PM   #5
astgtciv
Resu Deretsiger
 
astgtciv's Avatar
 
Join Date: Jul 2005
Location: St-Petersburg, Russia
Posts: 2,329
Default

Mm... donno about the the links, but the using the Object is pretty straightforward:

ActionScript Code:
var assocArray:Object = new Object(); 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 attached for (var i in assocArray) {    trace("index " + i + " contains " + assocArray[i]); }
astgtciv is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
problem removing item from an array lisaad ActionScript 2.0 2 04-29-2005 01:22 AM
problem creating mc instance names inside a function jasonyen ActionScript 2.0 10 12-08-2004 12:01 AM
removing a specified item from an array? Clox ActionScript 2.0 2 11-16-2004 05:11 AM
trouble with variable getting lost whoo Simple Stuff (Newbies) 2 08-07-2004 05:52 PM
Deleting a specific item of an array - works 100% ckey ActionScript 1.0 (and below) 3 02-07-2003 08:18 PM


All times are GMT. The time now is 11:36 PM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.