如何找到带有值的数组索引?

时间:2022-04-30 01:41:49

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。

10 个解决方案

#1


250  

you can use indexOf:

您可以使用indexOf:

var imageList = [100,200,300,400,500];
var index = imageList.indexOf(200); // 1

you will get -1 if it cant find a value in the array.

如果在数组中找不到值,就会得到-1。

#2


39  

For objects array use map with indexOf:

对象数组使用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 by Chrome, FF, Safari and (Unfortunately only) IE edge

它是ES6的一部分,支持Chrome, FF, Safari和(不幸的是)IE edge

#3


16  

Use jQuery's function jQuery.inArray

使用jQuery的jQuery.inArray函数

jQuery.inArray( value, array [, fromIndex ] )
(or) $.inArray( value, array [, fromIndex ] )

#4


12  

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);

#5


8  

Use indexOf

使用indexOf

imageList.indexOf(200)

#6


6  

how about indexOf ?

indexOf怎么样?

alert(imageList.indexOf(200));

#7


5  

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?

数组中。indexOf在某些版本的ie浏览器中并不适用——尽管有很多替代方法……请参见这个问题/答案:如何检查数组是否包含JavaScript中的对象?

#8


5  

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);

#9


4  

It is possible to use a ES6 function Array.prototype.findIndex.

可以使用ES6函数Array.prototype.findIndex。

MDN says:

MDN说:

The findIndex() method returns the index of the first element in the array that satisfies the 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');

#10


0  

// 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)

请注意:include函数是数组上的一个简单实例方法,可以帮助您轻松查找数组中的项(包括与indexOf不同的NaN)

#1


250  

you can use indexOf:

您可以使用indexOf:

var imageList = [100,200,300,400,500];
var index = imageList.indexOf(200); // 1

you will get -1 if it cant find a value in the array.

如果在数组中找不到值,就会得到-1。

#2


39  

For objects array use map with indexOf:

对象数组使用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 by Chrome, FF, Safari and (Unfortunately only) IE edge

它是ES6的一部分,支持Chrome, FF, Safari和(不幸的是)IE edge

#3


16  

Use jQuery's function jQuery.inArray

使用jQuery的jQuery.inArray函数

jQuery.inArray( value, array [, fromIndex ] )
(or) $.inArray( value, array [, fromIndex ] )

#4


12  

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);

#5


8  

Use indexOf

使用indexOf

imageList.indexOf(200)

#6


6  

how about indexOf ?

indexOf怎么样?

alert(imageList.indexOf(200));

#7


5  

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?

数组中。indexOf在某些版本的ie浏览器中并不适用——尽管有很多替代方法……请参见这个问题/答案:如何检查数组是否包含JavaScript中的对象?

#8


5  

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);

#9


4  

It is possible to use a ES6 function Array.prototype.findIndex.

可以使用ES6函数Array.prototype.findIndex。

MDN says:

MDN说:

The findIndex() method returns the index of the first element in the array that satisfies the 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');

#10


0  

// 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)

请注意:include函数是数组上的一个简单实例方法,可以帮助您轻松查找数组中的项(包括与indexOf不同的NaN)