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

前端 未结 30 1218

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

    Array.prototype.reverse() is all you need to do this work. See compatibility table.

    var myArray = [20, 40, 80, 100];
    var revMyArr = [].concat(myArray).reverse();
    console.log(revMyArr);
    // [100, 80, 40, 20]

提交回复
热议问题