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
---------------------------------------------------------------
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)
No comments:
Post a Comment