Skip to main content
Inspiring
May 11, 2013
Question

indexOf on multidimensional array

  • May 11, 2013
  • 1 reply
  • 407 views

var clipArray:Array = new Array();

clipArray[0] = new Array();

clipArray[1] = new Array();

clipArray[0][0]=2

clipArray[0][1]=3

clipArray[1][0]=4

clipArray[1][1]=5

trace(clipArray[0].indexOf(3));

This traces '1' cause 3 can be found in [0][1] of the multidimensional array and 1 is the second value of that 0.

Using indexOf this way I can determine that second value in which in this case the value 3 can be found.

I want to to take this one step further and use indexOf somehow to determine the first value of the clipArray variable. So looking for 2 or 3 should trace 0 and looking for 4 or 5 should trace 1. Is there some way to do this?

This topic has been closed for replies.

1 reply

kglad
Community Expert
Community Expert
May 11, 2013

the following returns both indices:

function indexOf2D(a:Array,v:*):Array{

for(var i:int=0;i<a.length;i++){

for(var j:int=0;j<a.length;j++){

if(a==v){

return [i,j];

}

}

}

return [-1,-1];

}