actionscript 3 - Efficient way to search for an element in an array based on its property? -


i have 3 variables, a, b , c. each of these variables have property foo if boolean.

out of a, b , c, 2 of them have foo property set true.

what's efficient way find out 1 has set false?

to put context, have function want stuff based on variable has foo set false, , not sure what's best way go determining false variable is.

e.g. manually check using if statements so:

function dothings():void     {         if (a.foo == true)         {             if (b.foo == true)             {                 true = a;                 true2 = b;                 false = c;             }             else             {                 true = a;                 true2 = c;                 false = b;             }         }         else         {             true = b;             true2 = c;             false = a;         }          //more things...     } 

or way thought of pass 2 variables know have foo set true arguments function, this:

function dothings(para, para2):void     {         if (a != para && != para2)         {             false = a;         }         else if (b != para && b != para2)         {             false = b;         }         else         {             false = c;         }          //more things...     } 

side note: 2 have foo set true pushed array @ runtime can't tell 1 programmatically, can pass them arguments function(s).

knowing this, check 1 isn't in array using indexof, fundamentals of method still remain same.

this done so, without using parameters:

function dothings():void     {         if (truearray.indexof(a) == -1)         {             false = a;             true = b;             true2 = c;         }         else if (truearray.indexof(b) == -1)         {             false = b;             true = c;             true2 = a;         }         else         {             false = c;             true = a;             true2 = b;         }          //more things...     } 

as stands, second method looks more appealing, , seems simplest, i'm wondering if there more sophisticated ways of doing this.

thanks reading.

i'd second option right direction. implement this:

function findfalsefoo(...objects:array):object {     each (var object:object in objects) {         if (object.foo == false)             return object;     } }  function dostuff():void {     var falsefoo:object = findfalsefoo(a, b, c);     switch (falsefoo) {         case a:             // stuff             break;         case b:             // stuff             break;         case c:             // stuff             break;     } } 

of course, in code you've posted you're doing in each case setting false, true, , true2 properties. in case this:

function assigntruefalse(...objects:array):void {     false = true = true1 = null;     each (var object:object in objects) {         if (object.foo) {             if (true == null)                  true = object;             else                 true2 = object;         } else {             false = object;         }     } }  assigntruefalse(a, b, c); 

Comments