Efficient way to search for an element in an array based on its property?
<p>I have three variables, <code>a</code>, <code>b</code> and <code>c</code>. Each of these variables have a property  <code>foo</code> which if is a <code>Boolean</code>. </p>

<p>Out of <code>a</code>, <code>b</code> and <code>c</code>, exactly two of them have their <code>foo</code> property set to <code>true</code>.</p>

<p>What's the most efficient way to find out which one has it set to <code>false</code>?</p>

<p>To put this into context, I have a function that I want to do some stuff based on which variable has <code>foo</code> set to <code>false</code>, and I am not sure what's the best way to go about determining what the <code>false</code> variable is.</p>

<p>E.g. I could manually check using if statements like so:</p>

<pre><code>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...
    }
</code></pre>

<p>Or another way I thought of was to pass the two variables that I know have <code>foo</code> set to true as arguments to the function, then do this:</p>

<pre><code>function doThings(para, para2):void
    {
        if (a != para &amp;&amp; a != para2)
        {
            False = a;
        }
        else if (b != para &amp;&amp; b != para2)
        {
            False = b;
        }
        else
        {
            False = c;
        }

        //More things...
    }
</code></pre>

<p>Side note: The two that have <code>foo</code> set to true are <code>push</code>ed into an array at runtime so I can't tell exactly which one is which programmatically, but I CAN pass them as arguments to the function(s).</p>

<p>Knowing this, I could also just as easily check which one isn't in the array using <code>indexOf</code>, but the fundamentals of the method would still remain the same.</p>

<p>This could be done like so, without using parameters:</p>

<pre><code>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...
    }
</code></pre>

<p>As it stands, the second method looks more appealing, and seems to be the simplest, but I'm just wondering if there are more sophisticated ways of doing this.</p>

<p>Thanks for reading.</p>