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

发布于 2024-12-03 19:45:19 字数 265 浏览 0 评论 0原文

假设我已经得到了这个,

imageList = [100,200,300,400,500];

这给了我

[0]100 [1]200 等。JavaScript

中有没有办法返回带有值的索引?

即我想要 200 的索引,我得到返回 1

Say I've got this

imageList = [100,200,300,400,500];

Which gives me

[0]100 [1]200 etc.

Is there any way in JavaScript to return the index with the value?

I.e. I want the index for 200, I get returned 1.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

就是爱搞怪 2024-12-10 19:45:19

您可以使用 indexOf

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

如果在数组中找不到值,您将得到-1。

You can use indexOf:

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

You will get -1 if it cannot find a value in the array.

菊凝晚露 2024-12-10 19:45:19

对于对象数组,请使用mapindexOf

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


在现代浏览器中,您可以使用 findIndex< /代码>

var imageList = [
   {value: 100},
   {value: 200},
   {value: 300},
   {value: 400},
   {value: 500}
];

var index = imageList.findIndex(img => img.value === 200);

console.log(index);

它是 ES6 的一部分,受 Chrome、FF、Safari 支持和边缘

For objects array use map with 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:

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 Edge

你丑哭了我 2024-12-10 19:45:19

使用jQuery的函数
jQuery.inArray

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

Use jQuery's function
jQuery.inArray

jQuery.inArray( value, array [, fromIndex ] )
(or) $.inArray( value, array [, fromIndex ] )
一念一轮回 2024-12-10 19:45:19

这是在 javascript 中在复杂数组中查找值索引的另一种方法。希望确实能帮助某人。
假设我们有一个 JavaScript 数组,如下所示,

var studentsArray =
     [
    {
    "rollnumber": 1,
    "name": "dj",
    "subject": "physics"
   },
   {
   "rollnumber": 2,
  "name": "tanmay",
  "subject": "biology"
   },
  {
   "rollnumber": 3,
   "name": "amit",
   "subject": "chemistry"
   },
  ];

现在如果我们需要选择数组中的特定对象。假设我们要查找名为 Tanmay 的学生的索引。

我们可以通过迭代数组并比较给定键的值来做到这一点。

function functiontofindIndexByKeyValue(arraytosearch, key, valuetosearch) {

    for (var i = 0; i < arraytosearch.length; i++) {

    if (arraytosearch[i][key] == valuetosearch) {
    return i;
    }
    }
    return null;
    }

您可以使用该函数查找特定元素的索引,如下所示,

var index = functiontofindIndexByKeyValue(studentsArray, "name", "tanmay");
alert(index);

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,

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.

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);
贪了杯 2024-12-10 19:45:19

使用索引

imageList.indexOf(200)

Use indexOf

imageList.indexOf(200)
辞取 2024-12-10 19:45:19

indexOf 怎么样?

alert(imageList.indexOf(200));

how about indexOf ?

alert(imageList.indexOf(200));
小女人ら 2024-12-10 19:45:19

Array.indexOf 在某些版本的 Internet Explorer 中不起作用 - 有很多替代方法可以做到这一点...请参阅此问题/答案:如何检查 JavaScript 中的数组是否包含对象?

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?

夜光 2024-12-10 19:45:19

当列表不是很长时,这是我知道的最好方法:

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

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);
℡寂寞咖啡 2024-12-10 19:45:19

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

MDN 说

findIndex() 方法返回数组中满足所提供的测试函数的第一个元素的索引。否则返回-1。

var fooArray = [5, 10, 15, 20, 25];
console.log(fooArray.findIndex(num=> { return num > 5; }));

// expected output: 1

通过对象属性查找索引。通过

对象属性查找索引:

yourArray.findIndex(obj => obj['propertyName'] === yourValue)

例如,有一个这样的数组:

let someArray = [
    { property: 'OutDate' },
    { property: 'BeginDate'},
    { property: 'CarNumber' },
    { property: 'FirstName'}
];

那么,查找必要属性索引的代码如下所示:

let carIndex = someArray.findIndex( filterCarObj=> 
    filterCarObj['property'] === 'CarNumber');

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

MDN says:

The findIndex() method returns the index of the first element in the array that satisfies the provided testing function. Otherwise -1 is returned.

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');
比忠 2024-12-10 19:45:19

在多维数组中


参考数组:

var array = [
    { ID: '100' },
    { ID: '200' },
    { ID: '300' },
    { ID: '400' },
    { ID: '500' }
];

使用 filterindexOf

var index = array.indexOf(array.filter(function(item) { return item.ID == '200' })[0]);

使用 循环遍历数组中的每个项目索引

for (var i = 0; i < array.length; i++) {
    var item = array[i];
    if (item.ID == '200') { 
        var index = array.indexOf(item);
    }
}

In a multidimensional array.


Reference array:

var array = [
    { ID: '100' },
    { ID: '200' },
    { ID: '300' },
    { ID: '400' },
    { ID: '500' }
];

Using filter and indexOf:

var index = array.indexOf(array.filter(function(item) { return item.ID == '200' })[0]);

Looping through each item in the array using indexOf:

for (var i = 0; i < array.length; i++) {
    var item = array[i];
    if (item.ID == '200') { 
        var index = array.indexOf(item);
    }
}
与君绝 2024-12-10 19:45:19

这是我的看法,似乎大多数人的解决方案都不会检查该项目是否存在,如果不存在,它会删除随机值。

首先通过查找其索引来检查该元素是否存在。
如果确实存在,请使用 splice 方法通过其索引将其删除

elementPosition = array.indexOf(value);

if(elementPosition != -1) {
  array.splice(elementPosition, 1);
}

Here is my take on it, seems like most peoples solutions don't check if the item exists and it removes random values if it does not exist.

First check if the element exists by looking for it's index.
If it does exist, remove it by its index using the splice method

elementPosition = array.indexOf(value);

if(elementPosition != -1) {
  array.splice(elementPosition, 1);
}
忘年祭陌 2024-12-10 19:45:19
// Instead Of 
var index = arr.indexOf(200)

// Use 
var index = arr.includes(200);

请注意:includes 函数是数组上的一个简单实例方法,有助于轻松查找数组中是否有某个项目(包括 NaN,与 indexOf 不同)

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文