Array#findLast
is available now in Node.js 18.
function foo(array, callback) {
for (let index = array.length - 1; index >= 0; index--) {
const element = array[index];
if (callback(element, index, array)) {
return element;
}
}
}
let foo;
for (let index = array.length - 1; index >= 0; index--) {
const element = array[index];
if (callback(element, index, array)) {
foo = element;
break;
}
}
array.findLast(callback);
This rule will be difficult to implement.
First, there are too many things in JavaScript that have .length
, we can't know the type of looping object.
Second, even if we assume they are all arrays, we still can only fix very few cases.
function foo(array, callback) {
for (let index = array.length - 1; index >= 0; index--) {
const element = array[index];
if (callback(element, index, array)) {
return element;
}
}
return somethingNotUndefiend; // This makes the case unfixable.
}
function foo(array, callback) {
for (let index = array.length - 1; index >= 0; index--) {
const element = array[index];
if (callback(element, index, array)) {
return element;
}
}
// Any additional code after this makes the case unfixable.
// Code handles not found case.
}
let foo = somethingNotUndefiend; // This makes the case unfixable.
for (let index = array.length - 1; index >= 0; index--) {
const element = array[index];
if (callback(element, index, array)) {
foo = element;
break;
}
}
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too