简体   繁体   English

如何在 30 个字节内反转没有 reverse() 的数组

[英]How to reverse an array without reverse() in 30 bytes

Reverse an array, return the result.反转一个数组,返回结果。 Do whatever you want with the original array.对原始数组做任何你想做的事情。 Don't use Array.prototype.reverse.不要使用 Array.prototype.reverse。

You have 30 bytes to spare.您有 30 个字节可用。

Example: [1, 2, 3] → [3, 2, 1]示例:[1, 2, 3] → [3, 2, 1]

And this time you won't be able to do the thing from that kata.而这一次,你将无法使用那个 kata 来做这件事。

require isn't allowed as well. require 也是不允许的。

You can create a copy of the input array, map over it, and use pop to get (and remove) the current last element of the original array.您可以在其上创建输入数组map的副本,然后使用pop获取(并删除)原始数组的当前最后一个元素。

 // 27 bytes f=a=>[...a].map(x=>a.pop()) console.log(f([1,2,3]));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM