I want to use the map() function on a javascript array, but I would like it to operate in reverse order.
The reason is, I\'m rendering stacked React co
Here is my TypeScript solution that is both O(n) and more efficient than the other solutions by preventing a run through the array twice:
function reverseMap(arg: T[], fn: (a: T) => O) {
return arg.map((_, i, arr) => fn(arr[arr.length - i - 1]))
}
In JavaScript:
const reverseMap = (arg, fn) => arg.map((_, i, arr) => fn(arr[arr.length - 1 - i]))
// Or
function reverseMap(arg, fn) {
return arg.map((_, i, arr) => fn(arr[arr.length - i - 1]))
}