有没有办法用 javascript 以相反的顺序在数组上使用 map() ?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36415904/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 19:02:19  来源:igfitidea点击:

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

javascriptarrays

提问by Robin Newhouse

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

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

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.

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

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

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

打印出来,a b c d e但我希望有一个 mapReverse() 打印出来e d c b a

Any suggestions?

有什么建议?

回答by AdamCooper86

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

回答by guest271314

You can use Array.prototype.reduceRight()

您可以使用 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)

回答by tyborg

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

根本不改变数组,这是我想出的单行 O(n) 解决方案:

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

回答by Harry

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

简写(没有命名回调函数)——箭头语法,ES6

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

Here is the result

这是结果

enter image description here

在此处输入图片说明

回答by edi9999

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

我更喜欢写一次 mapReverse 函数,然后使用它。这也不需要复制数组。

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 ]

回答by Madeo

Another solution could be:

另一种解决方案可能是:

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

You basically work with the array indexes

您基本上使用数组索引

回答by Duong Thanh Hop

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

一个老问题,但对于新观众来说,这是使用 map 反转数组的最佳方法

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

回答by dannielum

You can do myArray.reverse() first.

你可以先做 myArray.reverse() 。

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