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

前端 未结 30 1168

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

    Using ES6 rest operator and arrow function.

    const reverse = ([x, ...s]) => x ? [...reverse(s), x] : [];
    reverse([1,2,3,4,5]) //[5, 4, 3, 2, 1]
    

提交回复
热议问题