Javascript 如何找到具有值的数组索引?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7346827/
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
How to find the array index with a value?
提问by joedborg
Say I've got this
说我有这个
imageList = [100,200,300,400,500];
Which gives me
这给了我
[0]100 [1]200
etc.
[0]100 [1]200
等等。
Is there any way in JavaScript to return the index with the value?
JavaScript 中有什么方法可以返回带有值的索引吗?
I.e. I want the index for 200, I get returned 1.
即我想要200的索引,我得到返回1。
回答by voigtan
回答by Jaqen H'ghar
For objects arrayuse map
with indexOf
:
对于对象阵列使用map
与indexOf
:
var imageList = [
{value: 100},
{value: 200},
{value: 300},
{value: 400},
{value: 500}
];
var index = imageList.map(function (img) { return img.value; }).indexOf(200);
console.log(index);
In modern browsers you can use findIndex
:
在现代浏览器中,您可以使用findIndex
:
var imageList = [
{value: 100},
{value: 200},
{value: 300},
{value: 400},
{value: 500}
];
var index = imageList.findIndex(img => img.value === 200);
console.log(index);
Its part of ES6 and supported byChrome, FF, Safari and Edge
它是 ES6 的一部分,被Chrome、FF、Safari 和 Edge 支持
回答by Simon_Weaver
Use jQuery's function jQuery.inArray
使用 jQuery 的函数 jQuery.inArray
jQuery.inArray( value, array [, fromIndex ] )
(or) $.inArray( value, array [, fromIndex ] )
回答by Ravinder Singh Bhanwar
Here is an another way find value index in complex array in javascript. Hope help somebody indeed. Let us assume we have a JavaScript array as following,
这是在 javascript 中在复杂数组中查找值索引的另一种方法。希望确实可以帮助某人。让我们假设我们有一个 JavaScript 数组,如下所示,
var studentsArray =
[
{
"rollnumber": 1,
"name": "dj",
"subject": "physics"
},
{
"rollnumber": 2,
"name": "tanmay",
"subject": "biology"
},
{
"rollnumber": 3,
"name": "amit",
"subject": "chemistry"
},
];
Now if we have a requirement to select a particular object in the array. Let us assume that we want to find index of student with name Tanmay.
现在,如果我们需要在数组中选择一个特定的对象。让我们假设我们要查找名为 Tanmay 的学生的索引。
We can do that by iterating through the array and comparing value at the given key.
我们可以通过遍历数组并比较给定键的值来做到这一点。
function functiontofindIndexByKeyValue(arraytosearch, key, valuetosearch) {
for (var i = 0; i < arraytosearch.length; i++) {
if (arraytosearch[i][key] == valuetosearch) {
return i;
}
}
return null;
}
You can use the function to find index of a particular element as below,
您可以使用该函数来查找特定元素的索引,如下所示,
var index = functiontofindIndexByKeyValue(studentsArray, "name", "tanmay");
alert(index);
回答by TROODON
Use indexOf
使用 indexOf
imageList.indexOf(200)
回答by gion_13
how about indexOf
?
怎么样indexOf
?
alert(imageList.indexOf(200));
回答by Manse
Array.indexOf
doesnt work in some versions of internet explorer - there are lots of alternative ways of doing it though ... see this question / answer : How do I check if an array includes an object in JavaScript?
Array.indexOf
在某些版本的 Internet Explorer 中不起作用-尽管有很多替代方法可以做到这一点...请参阅此问题/答案:如何检查数组是否包含 JavaScript 中的对象?
回答by Ryan
When the lists aren't extremely long, this is the best way I know:
当列表不是很长时,这是我所知道的最好的方法:
function getIndex(val) {
for (var i = 0; i < imageList.length; i++) {
if (imageList[i] === val) {
return i;
}
}
}
var imageList = [100, 200, 300, 400, 500];
var index = getIndex(200);
回答by StepUp
It is possible to use a ES6
function Array.prototype.findIndex
.
可以使用ES6
函数Array.prototype.findIndex
。
The
findIndex()
method returns the index of the first element in the array that satisfiesthe provided testing function. Otherwise -1 is returned.
该
findIndex()
方法返回数组中满足提供的测试函数的第一个元素的索引。否则返回-1。
var fooArray = [5, 10, 15, 20, 25];
console.log(fooArray.findIndex(num=> { return num > 5; }));
// expected output: 1
Find an index by object property.
按对象属性查找索引。
To find an index by object property:
要按对象属性查找索引:
yourArray.findIndex(obj => obj['propertyName'] === yourValue)
For example, there is a such array:
例如,有一个这样的数组:
let someArray = [
{ property: 'OutDate' },
{ property: 'BeginDate'},
{ property: 'CarNumber' },
{ property: 'FirstName'}
];
Then, code to find an index of necessary property looks like that:
然后,查找必要属性索引的代码如下所示:
let carIndex = someArray.findIndex( filterCarObj=>
filterCarObj['property'] === 'CarNumber');
回答by Harshi Srivastava
// Instead Of
var index = arr.indexOf(200)
// Use
var index = arr.includes(200);
Please Note: Includes function is a simple instance method on the array and helps to easily find if an item is in the array(including NaN unlike indexOf)
请注意: Includes 函数是数组上的一个简单实例方法,有助于轻松查找数组中是否有项目(包括与 indexOf 不同的 NaN)