Is there a way to use map() on an array in reverse order with javascript?

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 components in a Meteor project and would like the top-level element to render first while the rest load images below.

var myArray = ['a', 'b', 'c', 'd', 'e'];
myArray.map(function (el, index, coll) { console.log(el + " ")
});

prints out a b c d e but I wish there was a mapReverse() that printed e d c b a

Any suggestions?

6

15 Answers

If you don't want to reverse the original array, you can make a shallow copy of it then map of the reversed array,

myArray.slice(0).reverse().map(function(...
8

Not mutating the array at all, here is a one-liner O(n) solution I came up with:

myArray.map((val, index, array) => array[array.length - 1 - index]);
2

You can use Array.prototype.reduceRight()

var myArray = ["a", "b", "c", "d", "e"];
var res = myArray.reduceRight(function (arr, last, index, coll) { console.log(last, index); return (arr = arr.concat(last))
}, []);
console.log(res, myArray)
1

With Named callback function

const items = [1, 2, 3];
const reversedItems = items.map(function iterateItems(item) { return item; // or any logic you want to perform
}).reverse();

Shorthand (without named callback function) - Arrow Syntax, ES6

const items = [1, 2, 3];
const reversedItems = items.map(item => item).reverse();

Here is the result

enter image description here

2

Another solution could be:

const reverseArray = (arr) => arr.map((_, idx, arr) => arr[arr.length - 1 - idx ]);

You basically work with the array indexes

2

I prefer to write the mapReverse function once, then use it. Also this doesn't need to copy the array.

function mapReverse(array, fn) { return array.reduceRight(function (result, el) { result.push(fn(el)); return result; }, []);
}
console.log(mapReverse([1, 2, 3], function (i) { return i; }))
// [ 3, 2, 1 ]
console.log(mapReverse([1, 2, 3], function (i) { return i * 2; }))
// [ 6, 4, 2 ]

You just need to add .slice(0).reverse() before .map()

students.slice(0).reverse().map(e => e.id)

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<T, O>(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]))
}

You can do myArray.reverse() first.

var myArray = ['a', 'b', 'c', 'd', 'e'];
myArray.reverse().map(function (el, index, coll) { console.log(el + " ")
});
2
function mapRevers(reverse) { let reversed = reverse.map( (num,index,reverse) => reverse[(reverse.length-1)-index] ); return reversed;
}
console.log(mapRevers(myArray));

I You pass the array to map Revers and in the function you return the reversed array. In the map cb you simply take the values with the index counting from 10 (length) down to 1 from the passed array

1

I think put the reverse() key after map is working.

tbl_products .map((products) => ( <div key={products.pro_id} > <article> <a href="#"> <img alt="Placeholder" src={products.product_img} /> </a> </article> </div> )) .reverse();

In my case, it is working

var myArray = ['a', 'b', 'c', 'd', 'e'];
var reverseArray = myArray.reverse()
reverseArray.map(function (el, index, coll) { console.log(el + " ")
});
1

If you are using an immutable array from let's say a redux reducer state, you can apply .reverse() by first making a copy like this:

const reversedArray = [...myArray].reverse();
//where myArray is a state variable
//later in code
reversedArray.map((item, i)=>doStuffWith(item))

First, you should make a shallow copy of the array, and then use that function on the copy.

[...myArray].reverse().map(function(...

An old question but for new viewers, this is the best way to reverse array using map

var myArray = ['a', 'b', 'c', 'd', 'e'];
[...myArray].map(() => myArray.pop());
2

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like