如何从末尾使用 FindIndex 循环遍历数组?
How to loop through array with FindIndex from its end?
我需要获取 errorIdx = 3
,但我得到的是 0。如何从数组的末尾开始循环?
const scrollPosition = 5007
const errorsHeight = [947, 2498, 3495, 4805, 5755]
errorIdx = errorsHeight.findIndex((itemHeight: number) => itemHeight < scrollPosition)
console.log(errorIdx) // 0
您可以只保留一个通过过滤器匹配项的数组,然后报告该数组的长度。我在这里添加了一个 sort
以确保数组在数字上是有序的。
const scrollPosition = 5007
const errorsHeight = [947, 2498, 3495, 4805, 5755]
errorIdx = errorsHeight
.sort((a, b) => a - b)
.filter(itemHeight => itemHeight < scrollPosition)
.length - 1
console.log(errorIdx)
对数组重新排序或反转它会为您的解决方案增加额外的 O(n)。
简单的方法是只做一个辅助函数,从数组的末尾开始查找索引:
function findLastIndex(arr, comparator){
for(let i = arr.length - 1; i > 0; i--){
const valid = comparator(arr[i], i);
if(valid){
return i;
}
}
return -1;
}
console.log(findLastIndex(errorsHeight, (itemHeight: number) => itemHeight < scrollPosition))
最好用一个简单的 for,像这样:
const scrollPosition = 5007;
const errorsHeight = [947, 2498, 3495, 4805, 5755];
let errorIdx = -1;
for (let i = 0; i < errorsHeight.length; i++)
if((errorIdx === -1 && errorsHeight[i] < scrollPosition) || (errorIdx >= 0 && errorsHeight[i] < scrollPosition && errorsHeight[i] > errorsHeight[errorIdx]))
errorIdx = i;
console.log(errorIdx) //3
;D
findLastIndex
的替代实现,其中回调函数知道 thisArg
context/target 并且还将使用其三个参数 [element, index, array]
调用;因此遵循 findIndex
...
的标准
function findLastIndex(arr, test, target) {
if (!arr && ((arr ?? true) === true)) {
throw new TypeError('findLastIndex called on null or undefined');
};
if (typeof test !== 'function') {
throw new TypeError(`${ test } is not a function`);
};
if (!Array.isArray(arr)) {
arr = Array.from(arr);
}
target = target ?? null;
let isContinue = true;
let idx = arr.length;
// assures -1 as return value for nothing found.
while ((idx >= 0) && isContinue) {
// be aware of sparse array slots ... and ...
// be a guard for the negative index default.
if (arr.hasOwnProperty(--idx)) {
isContinue = !test.call(target, arr[idx], idx, arr);
}
}
return idx;
}
const errorsHeight = [947, 2498, 3495, 4805, 5755];
const scrollPosition = 5007;
console.log(
findLastIndex(errorsHeight, (height/*, idx, arr*/) =>
height < scrollPosition
)
);
console.log(
findLastIndex(errorsHeight, height => height < 5)
);
.as-console-wrapper { min-height: 100%!important; top: 0; }
我需要获取 errorIdx = 3
,但我得到的是 0。如何从数组的末尾开始循环?
const scrollPosition = 5007
const errorsHeight = [947, 2498, 3495, 4805, 5755]
errorIdx = errorsHeight.findIndex((itemHeight: number) => itemHeight < scrollPosition)
console.log(errorIdx) // 0
您可以只保留一个通过过滤器匹配项的数组,然后报告该数组的长度。我在这里添加了一个 sort
以确保数组在数字上是有序的。
const scrollPosition = 5007
const errorsHeight = [947, 2498, 3495, 4805, 5755]
errorIdx = errorsHeight
.sort((a, b) => a - b)
.filter(itemHeight => itemHeight < scrollPosition)
.length - 1
console.log(errorIdx)
对数组重新排序或反转它会为您的解决方案增加额外的 O(n)。
简单的方法是只做一个辅助函数,从数组的末尾开始查找索引:
function findLastIndex(arr, comparator){
for(let i = arr.length - 1; i > 0; i--){
const valid = comparator(arr[i], i);
if(valid){
return i;
}
}
return -1;
}
console.log(findLastIndex(errorsHeight, (itemHeight: number) => itemHeight < scrollPosition))
最好用一个简单的 for,像这样:
const scrollPosition = 5007;
const errorsHeight = [947, 2498, 3495, 4805, 5755];
let errorIdx = -1;
for (let i = 0; i < errorsHeight.length; i++)
if((errorIdx === -1 && errorsHeight[i] < scrollPosition) || (errorIdx >= 0 && errorsHeight[i] < scrollPosition && errorsHeight[i] > errorsHeight[errorIdx]))
errorIdx = i;
console.log(errorIdx) //3
findLastIndex
的替代实现,其中回调函数知道 thisArg
context/target 并且还将使用其三个参数 [element, index, array]
调用;因此遵循 findIndex
...
function findLastIndex(arr, test, target) {
if (!arr && ((arr ?? true) === true)) {
throw new TypeError('findLastIndex called on null or undefined');
};
if (typeof test !== 'function') {
throw new TypeError(`${ test } is not a function`);
};
if (!Array.isArray(arr)) {
arr = Array.from(arr);
}
target = target ?? null;
let isContinue = true;
let idx = arr.length;
// assures -1 as return value for nothing found.
while ((idx >= 0) && isContinue) {
// be aware of sparse array slots ... and ...
// be a guard for the negative index default.
if (arr.hasOwnProperty(--idx)) {
isContinue = !test.call(target, arr[idx], idx, arr);
}
}
return idx;
}
const errorsHeight = [947, 2498, 3495, 4805, 5755];
const scrollPosition = 5007;
console.log(
findLastIndex(errorsHeight, (height/*, idx, arr*/) =>
height < scrollPosition
)
);
console.log(
findLastIndex(errorsHeight, height => height < 5)
);
.as-console-wrapper { min-height: 100%!important; top: 0; }