How to find an ArrayCollection element with a specific property value?

I have XML structures like this:

var struct:XML = <mh>
  <mi id="1" stuff="whatever"/>
  <mi id="2" stuff="whatever"/>
  <mi id="3" stuff="whatever"/>
</mh>;

I know that I can access the subzone using "id" this way:

var stuff:Object = struct.(hasOwnProperty('@id') && @id == '2').@stuff;

Now I have a similar ArrayCollection structure:

private var cmenu:ArrayCollection = new ArrayCollection([
    {id:"1", stuff:"whatever"},
    {id:"2", stuff:"whatever"},
    {id:"3", stuff:"whatever"}
]);

I wonder if the elements are available in a similar way, for example:

var stuff:Object = cmenu['id == 2'].stuff;

Is it possible?

+5
source share
7 answers

No, you can’t. struct.mi.(@id == "2").@stuffis E4X, which is short for ECMA Script for XML. You cannot use e4x for other AS objects.

+3
source

, , - , ( , ).

:

function findId(id:int):Function {
  return function( element : *, index : int, array : Array ) : Boolean
  {
    return element.id == id;
  }
}

, , :

function findInCollection(c:ArrayCollection, find:Function):Object {
  var matches : Array = c.source.filter( find );
  return ( matches.length > 0 ? matches[0] : null );
}

:

var stuff:String = findInCollection(cmenu, findId(2)) as String;
+18

filterFunctions ArrayCollections:

private var cmenu:ArrayCollection = new ArrayCollection([
    {id:"1", stuff:"whatever"},
    {id:"2", stuff:"whatever"},
    {id:"3", stuff:"whatever"}
]);

function getItemFromCollection(id:String):Object {
    var cmenuFiltered:ArrayCollection = new ArrayCollection(cmenu.toArray());

    cmenuFiltered.filterFunction =
        function(item:Object):Boolean {
            return item.id == id;
        }

    cmenuFiltered.refresh();

    return cmenuFiltered.getItemAt(0);
}
+4

Array, , , e4x, XML. filter(), , , .

, , .

var matches : Array = cmenu.source.filter( findId2 );
var stuff : Object = ( matches.length > 0 ? matches[0] : null );

... findId2:

function findId2( element : *, index : int, array : Array ) : Boolean
{
    return element.id == 2;
}
+3

findInCollection. , ArrayCollection, :

public function findInCollection(c:ArrayCollection, 
                       propertyName:String, propertyValue:*):Array {

    var matches : Array = c.source.filter( 
               function( element : *, index : int, array : Array ) : Boolean {
                 return element[propertyName] == propertyValue;
               } 
            );

    return matches; 

}

var ac:ArrayCollection=new ArrayCollection(
                [{name:'Ben', id:1, age:12},
                    {name:'Jack', id:2, age:22},
                    {name:'Jill', id:4, age:22},
                    {name:'Joe', id:3, age:17}
                ]
            );
var searchedElements:Array=findInCollection(ac,'age',22);

, "" "".

, , , , , .

+2

Herms, ​​ ArrayCollection

private function findInCollection(c:ArrayCollection, findFunction:Function):Array
{
 var matches : Array = c.source.filter( findFunction );
 return matches;
}

private function findFunction(propertyName:String,propertyValue:*):Function
{
   return function( element : *, index : int, array : Array ) : Boolean
   {
    return element[propertyName] == propertyValue;
   }
}

with the following use

var ac:ArrayCollection=new ArrayCollection(
    [{name:'Ben', id:1, age:12},
     {name:'Jack', id:2, age:22},
     {name:'Joe', id:3, age:17}
    ]
);
var searchedElements:Array=findInCollection(ac,findFunction('id',2));

it will return an array with the next object

{name:'Jack', id:2, age:22}

The disadvantage of this method is that we hard code the property name with String. This can be harmful to code maintenance.

0
source
'private var cmenu:ArrayCollection = new ArrayCollection([
    {id:"1", stuff:"whatever"},
    {id:"2", stuff:"whatever"},
    {id:"3", stuff:"whatever"}
]);

for(var i:int=0;i<cmenu.length;i++){
var num:Number=cmenu.getItemAt(i).id;
if(num==2){
var stuf:String=cmenu.getItemAt(i).stuff;
}
}'
0
source

All Articles