How to reverse an array in Javascript?

风格不统一 提交于 2019-12-13 11:21:32

问题


Some of you may know the method in Ruby that allows you to reverse an array:

arr = ['a', 'b', 'c']
puts arr.reverse

#=> ['c', 'b', 'a']

How would one write a function (or prototype if you wish) in Javascript?


回答1:


A quick glance at the array page in the manual shows a reverse method

> arr = ['a', 'b', 'c']; arr.reverse(); console.log(arr);
[ 'c', 'b', 'a' ]



回答2:


Use Array.reverse() method.




回答3:


function fun() {
    var c=["gh","jk","yu"];
    c.reverse();
    alert(c);
}


来源:https://stackoverflow.com/questions/9847171/how-to-reverse-an-array-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!