获取值大于x的数组中第一个元素的索引 [英] Get the index of the first element in an array with value greater than x
本文介绍了获取值大于x的数组中第一个元素的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个数组:
var array = [400, 4000, 400, 400, 4000];
如何获得值大于400的第一个元素的索引?
How can I get the index of the first element with value greater than 400?
注意:虽然确保这个问题是独一无二的,但我遇到了同样问题的问题 - 但对于不同的编程语言。
如果我的问题的副本适用于JS,我真的很想看到它们。
Note: While making sure this question was unique, I came across questions asking this same problem- but for different programming languages.
If there are duplicates of my question that apply to JS, I'd really want to see them.
推荐答案
你可以在这里使用 findIndex
检查此代码段
var array = [400, 4000, 400, 400, 4000];
var index=array.findIndex(function(number) {
return number > 400;
});
console.log(index);
这篇关于获取值大于x的数组中第一个元素的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文