JavaScript - 获取数组元素 position/index
JavaScript - Get array element position/index
我有这样的数组,例如:
const arr = [3,5,7,10];
而我有一个号码,例如:
const status = 4;
我必须找到这个数字的索引,所以数字 4 大于 3,小于 5,所以它属于第二个索引,我必须得到类似位置的东西。
数字的位置应该是2。(它大于3,但小于5)。
预期结果
const position = 2;
您在寻找这个简单的解决方案吗?
const arr = [3,5,7,10];
const status = 4;
const position = arr.findIndex( e => e > status) + 1;
console.log(position)
我有这样的数组,例如:
const arr = [3,5,7,10];
而我有一个号码,例如:
const status = 4;
我必须找到这个数字的索引,所以数字 4 大于 3,小于 5,所以它属于第二个索引,我必须得到类似位置的东西。
数字的位置应该是2。(它大于3,但小于5)。
预期结果
const position = 2;
您在寻找这个简单的解决方案吗?
const arr = [3,5,7,10];
const status = 4;
const position = arr.findIndex( e => e > status) + 1;
console.log(position)