如何用值找到数组索引? [英] How to find the array index with a value?

查看:114
本文介绍了如何用值找到数组索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有这个

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

这给了我

[0] 100 [1] 200 等。

JavaScript中是否有任何方法可以返回带有值的索引?

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

即我希望 200 的索引,我返回 1

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

推荐答案

您可以使用 indexOf

you can use indexOf:

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

如果它无法在数组中找到值,你将获得-1。

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

这篇关于如何用值找到数组索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆