I am curious if this is an okay implementation of the Array.filter()
method.
//Array of generic Object instances representing galleries.
//The images property is empty for the example
var galleries:Array = new Array();
galleries[0] = {name: 'Portraits', images: new Array()};
galleries[1] = {name: 'Landscapes', images: new Array()};
galleries[2] = {name: 'Still Lifes', images: new Array()};
//Returns a gallery Object by the specified name
function getGallery(galleryName:String):Object
{
function isGallery(element:*, index:int, arr:Array):Object
{
return (element.name == galleryName);
}
var arr:Array = galleries.filter(isGallery);
return arr[0];
}
var gallery:Object = getGallery('Landscapes');
if(gallery != null)
{
trace(gallery.name);
}
else
{
trace('Not found.');
}
This was the function I was using previously:
function getGallery(galleryName:String):Object
{
for each(var gallery:Object in galleries)
{
if(galleryName == gallery.name)
{
return gallery;
}
}
return null;
}
I was not able to figure out an implementation of the callback function for the filter()
method, where the callback was outside of the getGallery()
function. I wonder if there is a way to get the isGallery
function outside of the getGallery
scope?