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

前端 未结 30 1215

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条回答
  •  Happy的楠姐
    2020-11-27 06:22

    reverse array and sub-array (in place) with ES6.

    function reverse(array, i=0, j=array.length-1){
      while (i < j){
        [array[i], array[j]] = [array[j], array[i]];
        ++i;
        --j;
      }
    }
    

提交回复
热议问题