Reversing an array in different ways is one of the most popular questions you can expect to get at a JavaScript interview. In this article, I will present three ways to reverse an array, and using a built-in reverse
method is not going to be one of them. This method reverses an array by modifying it, which means the first element in the original array becomes the last element and the last one becomes the first. An array can be reversed by both modifying the original array or without changing the original array.
[R eversing以不同的方式数组是你可以期望获得在JavaScript的采访中最流行的问题之一。 在本文中,我将介绍三种反转数组的方法,而使用内置的reverse
方法将不是其中一种。 此方法通过修改数组来反转数组,这意味着原始数组中的第一个元素成为最后一个元素,而最后一个成为第一个元素。 可以通过修改原始数组或不更改原始数组来反转数组。
The first two solutions that I will share below will be examples of reversing an array without mutating the original array and the last one will demonstrate how to reverse an array by modifying the original array.
我下面将分享的前两个解决方案是在不更改原始数组的情况下反转数组的示例,最后一个将演示如何通过修改原始数组来反转数组。
1.使用For循环反转数组: (1. Reversing an Array with For Loop:)
We will use a decrementing loop for this approach to iterate through each element of a given array. The last element of the array will be the starting point (arr.length — 1)
of the loop and it will run until it reaches the beginning of the array (i ≥ 0)
.
我们将为这种方法使用递减循环,以迭代给定数组的每个元素。 数组的最后一个元素将是循环的起点(arr.length — 1)
,它将一直运行直到到达数组的起点(i ≥ 0)
。

reverseArray1
function takes an array (arr
) as an argument and creates a new array (newArr
) of the same elements in reverse order by looping over the given array backward (let i = arr.length - 1; i >= 0; i --
). This solution does not modify the original array because it pushes and stores the elements in a new array, which takes extra space.
reverseArray1
函数将一个数组( arr
)作为参数,并通过向后循环遍历给定的数组,以相反的顺序创建一个相同元素的新数组( newArr
)( let i = arr.length - 1; i >= 0; i --
)。 此解决方案不会修改原始数组,因为它会将元素推入并存储在新数组中,这会占用额外的空间。
2.使用Unshift()方法反转数组: (2. Reversing an Array with Unshift() Method:)
This approach is not very different than the first one in a way that it also uses an extra variable to store the reversed array, thus, the original array remains unmodified.
这种方法与第一种方法没有很大不同,因为它还使用一个额外的变量来存储反向数组,因此,原始数组保持不变。

reverseArray2
function loop over the given array (arr
) from the beginning to the end. It uses the unshift
method on the new array (newArr
) and inserts each element to the start position (newArr[0]
) of the array. This second solution is also less space efficient like the first one since it requires more memory to store the reversed array in an additional variable (newArr
).
reverseArray2
函数从头到尾遍历给定数组( arr
)。 它在新数组( newArr
)上使用unshift
方法,并将每个元素插入到数组的开始位置( newArr[0]
)。 与第一个解决方案相比,第二个解决方案的空间效率也较低,因为它需要更多内存才能将反向数组存储在其他变量( newArr
)中。
3.就地反转阵列: (3. Reversing an Array In-Place:)
Similar to reverse
method, our last approach also modifies the original array by reversing its elements in-place. This solution; reversing an array in-place, is more complicated than the first two solutions.
与reverse
方法类似,我们的最后一种方法也通过在原位反转其元素来修改原始数组。 这个解决方案; 与前两个解决方案相比, 就地反转数组要复杂得多。

In the code above, we loop through the half of the elements of the given array by using Math.floor
to round down (i < Math.floor(arr.length/2)
). And then swap the array’s element in the first place with the last place, second place with the second to the last place and so on. Instead of using a local binding, we used array destructuring to swap its elements.
在上面的代码中,我们使用Math.floor
向下舍入( i < Math.floor(arr.length/2)
) i < Math.floor(arr.length/2)
给定数组的一半元素。 然后将数组的元素放在第一位和最后一位,第二位与第二位到最后一位,依此类推。 代替使用局部绑定,我们使用数组解构来交换其元素。
结论: (Conclusion:)
In this blog post, we have discussed how to reverse an array by utilizing a for loop
, by using an unshift
method, and by implementing an in-place solution. Although the time complexity of all these solutions will not be different than each other; O(n), the space complexity of the last approach will be constant; O(1) unlike the first two; O(n).
在此博客文章中,我们讨论了如何通过使用for loop
,使用unshift
方法以及实现就地解决方案来反转数组。 尽管所有这些解决方案的时间复杂度不会有所不同; O(n) ,最后一种方法的空间复杂度将是恒定的; O(1)与前两个不同; 上)。
There are many other ways to reverse an array in JavaScript, but I shared my three favorite solutions. Thank you for taking the time to read my post and I hope you found this helpful! Feel free to check out my other articles on JavaScript algorithms:
还有许多其他方法可以用JavaScript反转数组,但是我分享了三个我最喜欢的解决方案。 感谢您抽出宝贵的时间阅读我的文章,希望对您有所帮助! 随时查看我关于JavaScript算法的其他文章:
翻译自: https://medium.com/swlh/javascript-three-ways-to-reverse-an-array-32f9819e9a97