【发布时间】:2022-01-06 16:48:38
【问题描述】:
我需要得到errorIdx = 3,但我得到了0。如何从数组的末端循环遍历?
const scrollPosition = 5007
const errorsHeight = [947, 2498, 3495, 4805, 5755]
errorIdx = errorsHeight.findIndex((itemHeight: number) => itemHeight < scrollPosition)
console.log(errorIdx) // 0
【问题讨论】:
-
用
array.reverse()反转数组,但如果你想保留原始数组的索引。然后进行克隆并反转它,然后一旦发现它的索引反转,在原始数组中搜索元素 -
@MrJami 正是我要说的
-
或者使用
for循环:for (let i = errorsHeight.length-1; i >= 0; i--) {...} -
在这种情况下errorIdx = 1,但我需要它为3 ....不能使用reverse()
-
@qweezz 将我的评论读到最后。你仍然可以得到你的 3。
标签: javascript arrays loops search callback