Javascript - 获取数组元素的位置 [英] Javascript - Get position of the element of the array

查看:121
本文介绍了Javascript - 获取数组元素的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,一个名为arrayElements的数组类型的变量包含[1,2,3,4]。

For instance, a variable named arrayElements of array type contains [1,2,3,4].

我如何获得具有数组变量中的值3除了使用循环?

How do i get the position of which that has the value "3" in the array variable besides using loop?

谢谢。

推荐答案

考虑使用 indexOf ,它返回数组中元素的从0开始的位置。

consider using indexOf, it returns the 0-based position of the element in the array.

例如

[1,2,3,4].indexOf(3); // produces 2

请注意,此方法在IE 8及以下版本中不可用。

Note that this method is not available in IE 8 and below.

这篇关于Javascript - 获取数组元素的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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