views:

55

answers:

3
var asdf:Array = [ [1,1] ];
trace( asdf.indexOf( [1,1] ) ); // -1

Why can't indexOf() find the [1,1] array?

A: 

this works. probably because the inner array is typed.

var qwer:Array = [1,1];
var asdf:Array = [qwer];
trace( asdf.indexOf( qwer ) ); // 0
shi11i
No it suceeds because qwer is only one array reference
Patrick
A: 

It fails because when you do a [x,y] you are creating a new array, adsf contains one array and indexOf search for another one.

try:

trace([1,1] == [1,1]);

You will see that it prints false, since array are compare by reference.

One quick indexOf function, arrange it to suit your needs:

function isElmEquals(e1:*, e2:*):Boolean {
    return (e1==e2);
}

function isArrayEquals(a1:Array, a2:Array):Boolean {
    if (a1==a2)
        return true;

    if ((a1==null) || (a2==null)) {
        return false;
    }

    if (a1.length!=a2.length)
        return false;

    for (var i:int=0;i<a1.length;i++){
        if (!isElmEquals(a1[i], a2[i]))
            return false;
    }

    return true;
}

function indexOf(value:Array, into:Array):int{
    var i:int = -1;
    into.some(
        function(item:*, index:int, array:Array):Boolean {
            if (isArrayEquals(item as Array, value)) {
                i = index;
                return true;
            }
            return false;
        }
    );
    return i;
}

var i:int=indexOf([1,1], [[-1,1], [0,1], [1,1], [1,-1]]);
trace(i);

var j:int=indexOf([1,2], [[-1,1], [0,1], [1,1], [1,-1]]);
trace(j);
Patrick
A: 

Here is a little function I wrote a while ago that works great. I included a lot of comments and an example search/function to output the results.

// set up a multidimensional array that contains some data
var myArray:Array = new Array();
myArray.push(["granola","people... are great"," 4 ","10"]);
myArray.push(["bill","orangutan","buster","keaton"]);
myArray.push(["steve","gates","24","yes, sometimes"]);
myArray.push(["help","dave","jobs","hal"]);

// here we set up some properties on the array object to hold our search string and our results
myArray.myTarget = "steve";
myArray.myResults = [];

// now we call the search
myArray.forEach(multiSearch);

// this is the function that does all the heavy lifting....
function multiSearch(element:*, index:int, array:Array)
{
    // see if we have a match in this array and pass back its index
    for(var i:* in element)
    {   
        if( element[i].indexOf( array.myTarget ) > -1 )
        {
            var tempArray:Array = array.myResults;
            tempArray.push([index,i]);
            array.myResults = tempArray;
        }
    }
}

// -------------------------------------------------------------------------------
// all the code below is OPTIONAL... it is just to show our results
// in the output window in Flash so you know it worked....
var printArray:Array = myArray.myResults;
for(var i:* in printArray)
{
    trace("TARGET FOUND @: "+printArray[i][0]+", "+printArray[i][1]+" = "+myArray[ printArray[i][0] ][ printArray[i][1] ]); 
}
// -------------------------------------------------------------------------------
exoboy
This is the bare-bones version (the only stuff you need to make it work, except for you array index content): var myArray:Array = new Array(); myArray.myTarget = "steve"; myArray.myResults = []; myArray.forEach(multiSearch); function multiSearch(element:*, index:int, array:Array) { for(var i:* in element) { if( element[i].indexOf( array.myTarget ) > -1 ) { var tempArray:Array = array.myResults; tempArray.push([index,i]); array.myResults = tempArray; } } }
exoboy
Thanks. I just added a tiny new function ( asdf.find = function(){} ) inspired by your solution.
embassyhill
Great! It is tough to always be able to interpret what bit people need... so I am always happy when a solution helps!
exoboy