Question

Ok. so I'm working on an app that retrieves items from a db and builds a gallery. I've done this a ton of times, and it should be simple.

I'm running into problems, because in this gallery, I get results from a db that includes both image files, and other files. Let's just say I can't change anything but the flash, so I need to detect if it's an image, and only display it if it is.

My question is: How the hell can I delete a property from an object without the object staying the same size? I use a count() function to generate pagination data, so I can't just 'null' them, and as I understand it, delete() is not an option either.

My solution for this was to just create another object, filter the good items with a for in loop, then pop them in to another object, but each item in the object is an object, and I have no push() function for objects.

So, in desperation, I am using an increment to add the objects to the new object using an index (goodItemsObject[index] = allItemsObject[object]), but that seems like a really gruesome way of getting around this problem.

Here's some code:

var filteredMO = new Object();
var newFile = 0;
for each(var file in mediaObject){
if(check_file(file)){
    filteredMO[newFile] = file;
    newFile++;
}
}
mediaObject = filteredMO;

check_file() just returns true or false, mediaObject is all full of objects.

I'd much prefer to be doing this:

for each(var file in mediaObject){
    if(check_file(file)){
         //remove_from_object_for_reals(mediaObject[file]);
    }
}

I realize that might not be possible (would it throw off the for loop?), but something similar would be sweet. I'd love to be able to let the gc grab all these useless objects hanging out.

any ideas?

thanks,

Jesse

Was it helpful?

Solution

What you are using to store your object now is called an associative array. You cannot remove a key-value from an associative array. What you need is a dictionary (which as3 has a build-in one http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/utils/Dictionary.html).

import flash.utils.Dictionary;

var dict:Dictionary = new Dictionary();

then you can remove a key from your dictionary by calling

delete dict["myKey"]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top