Get the index of the first element in an array with value greater than x

前端 未结 3 1046
有刺的猬
有刺的猬 2021-02-09 16:53

I have this array:

var array = [400, 4000, 400, 400, 4000];

How can I get the index of the first element with value greater than 400?

3条回答
  •  爱一瞬间的悲伤
    2021-02-09 17:26

    You can use findIndex here

    check this snippet

    var array = [400, 4000, 400, 400, 4000];
    var index=array.findIndex(function(number) {
      return number > 400;
    });
    console.log(index);

提交回复
热议问题