有没有办法在javascript上以相反的顺序在数组上使用map()? [英] Is there a way to use map() on an array in reverse order with javascript?

查看:57
本文介绍了有没有办法在javascript上以相反的顺序在数组上使用map()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在javascript数组上使用 map()函数,但我希望它以相反的顺序运行。

I want to use the map() function on a javascript array, but I would like it to operate in reverse order.

原因是,我在Meteor项目中渲染堆叠的React组件,并希望顶层元素首先渲染,而其余元素在下面加载。

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 + " ")
});

打印出 abcde 但我希望有mapReverse()打印 edcba

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

有任何建议吗?

推荐答案

如果您不想反转原始数组,可以对其进行浅层复制,然后反转数组的映射,

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(...

这篇关于有没有办法在javascript上以相反的顺序在数组上使用map()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆