问题
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