繁体   English   中英

有没有什么简单的方法可以在不循环的情况下从头到尾对数组进行切片?

[英]Is there any short way to slice an array from end to start without looping?

例如我有这个数组

[1,2,3,4]

我写了这段代码,但它也很长。 你能帮我找到一个更短的解决方案吗?

array.map((e,i)=>array[array.length-1-i])

如何在不使用反向方法并且使用比我的代码更短的方法的情况下反转数组。

 let array = [1, 2, 3, 4]; //this is shorter but more expensive const reverse = array.sort(x => -1); console.log(...reverse); //if you want to keep the original array intact, make a copy of the array and then sort it //const reverse = [...array].sort(x => -1);

暂无
暂无

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

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