javascript 使用 indexOf 方法在二维数组中查找值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11080736/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using indexOf method to find value in a 2D array
提问by A_user
I want to ask a question that I have about a 2D array in JavaScript, similar to this:
我想问一个关于 JavaScript 中二维数组的问题,类似于:
var array = [[2,45],[3,56],[5,67],[8,98],[6,89],[9,89],[5,67]]
That is, on every index, I have an array as well.
也就是说,在每个索引上,我也有一个数组。
Suppose that I have a value in my second index like 56
, and I want the corresponding 1st index (i.e. 3 in above example).
假设我的第二个索引中有一个值,例如56
,并且我想要相应的第一个索引(即上例中的 3)。
I can do it using a loop (if no other option), but is there any better way?
我可以使用循环来完成(如果没有其他选择),但有没有更好的方法?
Also, I know indexOf
method in JavaScript, but it's not returning the index when I call it like this:
另外,我知道indexOf
JavaScript 中的方法,但是当我这样调用它时它没有返回索引:
array.indexOf(56);
Any insight will be helpful.
任何见解都会有所帮助。
回答by Florian Margaine
Use some iterator function.
使用一些迭代器函数。
filter
allows you to iterate over an array and returns only the values when the function returns true
.
filter
允许您遍历数组并仅在函数返回时返回值true
。
Modern browsers way:
现代浏览器方式:
var arr = array.filter( function( el ) {
return !!~el.indexOf( 56 );
} );
console.log( arr ); // [3, 56]
console.log( arr[ 0 ] ); // "3"
Legacy browsers way, using jQuery:
传统浏览器方式,使用 jQuery:
var arr = $.filter( array, function() {
return !!~$.inArray( 56, $( this ) );
} );
console.log( arr ); // [3, 56]
console.log( arr[ 0 ] ); // "3"
回答by xdazz
You could do like this:
你可以这样做:
var ret;
var index = array.map(function(el){return el[1];}).indexOf(56);
if (index !== -1) {
ret = array[index][0];
}
回答by DavidHyogo
I found that in IE6 at least (I'm not sure about the more recent versions), the built-in Array type didn't have an indexOf method. If you're testing in IE, perhaps that's why you couldn't get started. I wrote this little addition to include in all my code:
我发现至少在 IE6 中(我不确定更新的版本),内置的 Array 类型没有 indexOf 方法。如果您在 IE 中进行测试,也许这就是您无法开始的原因。我写了这个小补充以包含在我的所有代码中:
if(!Array.prototype.indexOf){
Array.prototype.indexOf = function(obj){
for(var i=0; i<this.length; i++){
if(this[i]===obj){
return i;
}
}
return -1;
};
}
I can't really take credit for this. It's probably a fairly generic piece of code I found in some tutorial or one of Doug Crockford's articles. I'm quite sure someone will come along with a better way of writing it.
我真的不能相信这一点。这可能是我在一些教程或 Doug Crockford 的一篇文章中找到的一段相当通用的代码。我很确定有人会想出更好的写作方式。
I find working in Firefox is a good place to start because you can install the Firebug console and get a better idea where things are going wrong. I admit that doesn't solve cross-browser problems, but at least it allows you to get a start on your project.
我发现在 Firefox 中工作是一个很好的起点,因为您可以安装 Firebug 控制台并更好地了解哪里出错了。我承认这并不能解决跨浏览器的问题,但至少它能让你开始你的项目。
This little piece of code will ensure you can use the indexOf method to find where a given value is in a one-dimensional array. With your 2-dimensional array, each element in the outer array is an array not a single value. I'm going to do a little test to see exactly how that works and get back to you, unless someone else comes in with a better answer.
这段代码将确保您可以使用 indexOf 方法来查找给定值在一维数组中的位置。对于二维数组,外部数组中的每个元素都是数组而不是单个值。我将做一个小测试,看看它到底是如何工作的,然后再回复你,除非其他人提供了更好的答案。
I hope this at least gets you started.
我希望这至少能让你开始。
UPDATE Assuming you have a browser that implements Array.indexOf, I've tried every which way to pass one of the array elements in your outer array.
更新 假设您有一个实现 Array.indexOf 的浏览器,我已经尝试了每种方法来传递外部数组中的数组元素之一。
alert(array.indexOf([3,56]))
alert(array.indexOf("3,56"))
just return -1.
只需返回-1。
Oddly,
奇怪,
alert(array.indexOf(array[1]))
correctly returns 1. But you need to know the index of the inner array to get the index! I can't see a way of passing an array to the indexOf method.
正确返回 1。但是你需要知道内部数组的索引才能得到索引!我看不到将数组传递给 indexOf 方法的方法。
回答by nbrooks
array.indexOf( x )
returns the position of x
in the array, or -1 if it is not found. In your 2D array this is useless, since 56 isn't in array; it is in an array which is contained in array
.
array.indexOf( x )
返回x
数组中的位置,如果未找到,则返回-1。在您的二维数组中,这是无用的,因为 56 不在数组中;它位于包含在array
.
You will have to loop over the first array, then use indexOf
to check each sub-array.
您必须遍历第一个数组,然后使用它indexOf
来检查每个子数组。