的indexOf语法多维数组? [英] indexOf syntax for multidimensional arrays?
问题描述
什么是的indexOf()的语法
要经过一个多维数组?
例如:
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) // ??
我想找到 A
',做的东西 B
。但它不工作......由于这种方法应该是反复的本身,我没有使用任何其他迭代的是做一件好事presume。目前我模拟这种使用2个简单的数组,但我想这应该以某种方式工作太...
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...
推荐答案
简单:的indexOf()
不以这种方式工作。如果你做了这样的事情可能工作:
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);
但后来你已经有z.b不是吗?所以,如果你必须忽略大家谁认为使用一个对象(或字典)其实更容易,你必须要么使用的阿泰古拉尔的<一个href=\"http://stackoverflow.com/questions/1427640/indexof-syntax-for-multidimensional-arrays/1428008#1428008\">approach,或搜索索引自己:
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
这篇关于的indexOf语法多维数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!