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

前端 未结 30 1180

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:15

    Pure functions to reverse an array using functional programming:

    var a = [3,5,7,8];
    
    // ES2015
    function immutableReverse(arr) {
      return [ ...a ].reverse();
    }
    
    // ES5
    function immutableReverse(arr) {
      return a.concat().reverse()
    }
    

提交回复
热议问题