JavaScript实例 - 反转数组、字符串 reverse

var arr = ["apple","banana","orange"];
方法一:
var stringReverse = function(arr) {
    var len=arr.length,x=arr,str="";
    for(var i=len-1;i>=0;i--){
        str+=","+x[i];
    }
    return str;
}
var newArr1 = stringReverse(arr).slice(1).split(",");
console.log(newArr1);
方法二:
var newArr2 = arr.slice(0).reverse();
console.log(newArr2);
方法三:
var func = function(x,indx,str){
    return indx == 0 ? str : func(x,--indx,(str +=" " + x[indx]));
}
var arrNew3 = func(arr,arr.length,"");
console.log(arrNew3);
 
  
 
 

你可能感兴趣的:(CSS,Javascript)