Javascript - 获取数组元素的位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8863549/
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
Javascript - Get position of the element of the array
提问by Jia-Luo
For instance, a variable named arrayElements of array type contains [1,2,3,4].
例如,数组类型的名为 arrayElements 的变量包含 [1,2,3,4]。
How do i get the position of which that has the value "3" in the array variable besides using loop?
除了使用循环之外,如何获得数组变量中值为“3”的位置?
thanks.
谢谢。
回答by qiao
consider using indexOf
, it returns the 0-based position of the element in the array.
考虑使用indexOf
,它返回数组中元素从 0 开始的位置。
e.g.
例如
[1,2,3,4].indexOf(3); // produces 2
Note that this method is not available in IE 8 and below.
请注意,此方法在 IE 8 及以下版本中不可用。
回答by zaphod1984
jQuery provides yout with an "inArray" functionality, similar to PHP:
jQuery 为您提供“inArray”功能,类似于 PHP:
var a = [1,2,3,4];
console.log( jQuery.inArray(3, a) ); //returns 2
If you're already using jQuery in your project, this would be the way to go! jQuery uses indexOf (the fastest solution) if possible and steps back to a loop based solution if not.
如果您已经在您的项目中使用 jQuery,那么这将是您的最佳选择!如果可能,jQuery 使用 indexOf(最快的解决方案),如果没有,则退回到基于循环的解决方案。
回答by Sebastian Stiehl
Or simple use a for loop:
或者简单地使用 for 循环:
function getPosition(elementToFind, arrayElements) {
var i;
for (i = 0; i < arrayElements.length; i += 1) {
if (arrayElements[i] === elementToFind) {
return i;
}
}
return null; //not found
}
getPosition(3, [1, 2, 3, 4]);
回答by hugomg
As was already pointed out by some answers, arrays in newer browsers have methods like indexOf
map
and forEach
that are veryconvenient for avoiding writing lots of for-loops.
正如一些答案已经指出的那样,较新的浏览器中的数组具有像这样的方法indexOf
map
,forEach
这对于避免编写大量 for 循环非常方便。
If you need to support old environments without these functions I would strongly recommendusing a library (or writing one of your own) to implement those very common array operations (kind of how soc suggested).
如果您需要在没有这些功能的情况下支持旧环境,我强烈建议您使用库(或编写您自己的库)来实现那些非常常见的数组操作(类似于 soc 建议的那种)。
var arrayfn = (function(){
var M = {};
M.indexOf = function(array, item){ ... };
M.contains = function(array, item){ return M.indefOf(array, item) !== -1; };
M.map = function(array, callback){ ... };
}());
arrayfn.contains([1,2,3], 2);
Most popular JS frameworks should already have most of these builtin anyway. Out of the top of my head I remember Dojo, jQuery and Underscore have some of these functions, for example.
大多数流行的 JS 框架应该已经内置了其中的大部分。例如,在我的脑海中,我记得 Dojo、jQuery 和 Underscore 有一些这样的功能。
回答by robe007
This example only demonstrate that is possible the use of map()
to return each element of the array. Once we have it, we retrieve the element's index in the array:
此示例仅演示了可以使用map()
返回数组的每个元素。一旦我们有了它,我们就检索数组中元素的索引:
function searchPosition(elementToFind, array) {
return array.map(function(el) {
return el;
}).indexOf(elementToFind);
}
searchPosition(3, [1, 2, 3, 4]); // returns 2
BONUS:
奖金:
This is not a part of the OP's question, but if you want to retrieve the position of the first element in the array that satisfies certain condition, you can do it with the findIndex()
function provided in the ES6:
这不是 OP 问题的一部分,但是如果您想检索满足特定条件的数组中第一个元素的位置,您可以使用findIndex()
ES6 中提供的函数来完成:
var array = [101, 90, 104, 80];
function findFirstLessNumber(element) {
return element < 100;
}
console.log(array.findIndex(findFirstLessNumber)); // returns 1 (the position of 90)