views:

655

answers:

6

What is the syntax for indexOf() to go through a multidimensional array? For instance:

var x = [];
// do something
x.push([a,b]);

x.indexOf(a) // ??

I want to find 'a' and do something with 'b'. But it does not work... As this method should be iterative itself, I do not presume using any other iteration would be a good thing to do. Currently I simulate this using 2 simple arrays but I guess this should somehow work too...

A: 

JavaScript does not have multidimensional arrays as such, so x is merely an array. Also, the indexOf method of Array is not supported in all browsers, in particular IE (up to version 7, at least), only being introduced in JavaScript 1.6.

Your only option is to search manually, iterating over x and examining each element in turn.

Tim Down
JavaScript has arrays of arrays: `z=[[1,2],[3,4,5]];` I know that's not a multidimensional array, but just be clear the the asker that his problem is nomenclature, not specifically that he couldn't `push` `[a,b]` onto `x`.
dlamblin
True. My point was that indexOf can't be expected to do any kind of clever recursive search in an array of arrays (which is what it seemed to me the asker was hoping for) since there is no explicit support for multidimensional arrays in the language.
Tim Down
+1  A: 

Unless you want to preserve order, it's much simpler to use a dictionary:

var x = {};

// do something

x[a] = b;

You can still iterate over the keys, although the order is undefined:

for (var key in x) {
   alert(x[key]);
}
Philippe Leybaert
Use x[a] instead of x.a. "a" is not the key name, but the variable that holds the key.
Ates Goral
true. I'll fix that.
Philippe Leybaert
A: 
x[ x.indexOf(a) ]

This will only work with ordered objects/lists and only if Array.prototype.indexOf is defined.

x = [1,2], b = {one:function(){alert('gj')}}, c = x.push(b);

x[ x.indexOf( b ) ]['one']()
meder
I tried this, it does not work for some reason...
jirkap
Well, you must've did it differently because the way I did it works. I did note that this *ONLY* works on arrays that are numerically ordered and have a length property, NOT on "multidimensional" or dictionaries.
meder
Note - you also need to have a reference to the original object for the reference you're feeding to 'b', a similar object with similar properties is still different.
meder
+1  A: 

You could use a an object (or dictionary) as Philippe Leybaert suggested. That will allow quick access to elements:

var x = {};

x[a] = b; // to set

alert(x[a]) // to access

But if you insist on using indexOf, there's still a way, however ugly it is:

var x = [];

var o = Object(a); // "cast" to an object
o.b = b; // attach value

x.push(o);

alert(x.indexOf(a)); // will give you the index
alert(x[1].b); // access value at given index
Ates Goral
A: 

So, basically, it is not doable. The Object way is unnecessarily complicated in this case, I want to stick with indexOf because of its simplicity, therefore I will keep my two arrays. Thanks to everyone of course.

jirkap
You do know that `indexOf` is not very well supported (e.g. not implemented in IE), don't you?
kangax
I do, it does not matter this time..
jirkap
A: 

Simply: indexOf() does not work this way. It might work if you did something like this:

var x = [];
// do something
z = [a,b];
x.push(z);

x.indexOf(z);

But then you'd already have z.b wouldn't you? So if you must ignore the advice of everyone who thinks that using an object (or dictionary) is actually easier you'll have to either use Ates Goral's approach, or search for the index yourself:

Array.prototype.indexOf0 = 
  function(a){for(i=0;i<this.length;i++)if(a==this[i][0])return i;return null;};
var x = [];
// do something
x.push([a,b]);

x.indexOf0(a); //=> 0
dlamblin