Javascript - Get position of the element of the array

前端 未结 5 1431
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-13 20:36

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

How do i get the position of which that has the value \"3\" in the array variable besi

5条回答
  •  隐瞒了意图╮
    2020-12-13 21:11

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

    e.g.

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

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

提交回复
热议问题