How can I reverse an array in JavaScript without using libraries?

前端 未结 30 1209

I am saving some data in order using arrays, and I want to add a function that the user can reverse the list. I can\'t think of any possible method, so if anybo

30条回答
  •  情歌与酒
    2020-11-27 06:23

    We have reverse() function to reverse the given array in JS.

    var a = [7,8,9];
    a.reverse(); // 9 8 7
    
    function reverseArr(input) 
    {
    var ret = new Array;
    for(var i = input.length-1; i >= 0; i--) 
    {
        ret.push(input[i]);
    }
    return ret;
    }
    

提交回复
热议问题