如何找到具有值的数组索引? [英] How to find the array index with a value?
本文介绍了如何找到具有值的数组索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
说我有这个
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 cannot find a value in the array.
这篇关于如何找到具有值的数组索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文