In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "JavaScript how to achieve array inversion operation", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "JavaScript how to achieve array inversion operation" bar!
How to use the Reverse method to reverse arrays in JavaScript
When you need to reverse an array in JavaScript, you can use the reverse method, which places the last element first and the first element last:
Let numbers = [1,2,3,4,5]; let reversedNumbers = numbers.reverse (); console.log (reversedNumbers); / / [5,4,3,2,1]
Remember, however, that the reverse method also modifies the original array:
Let numbers = [1,2,3,4,5]; let reversedNumbers = numbers.reverse (); console.log (reversedNumbers); / / [5,4,3,2,1] console.log (numbers); / / [5,4,3,2,1]
Some coding challenges may want you to keep the original array, so let's see how to reverse the array without changing the original array.
How to use the Spread operator to reverse arrays in JavaScript
You can use the extension operator (Spread) in conjunction with the reverse method to reverse the array without changing the original array.
First, put the elements returned by the spread operator into the new array by enclosing the spread syntax in square brackets []:
[... numbers]
Then, you call the reverse method on the array. In this way, the reverse method is executed on the new array instead of the original array:
Let numbers = [1,2,3,4,5]; let reversedNumbers = [... numbers] .reverse (); console.log (reversedNumbers); / / [5,4,3,2,1] console.log (numbers); / / [1,2,3,4,5]
Note: the spread method is the ES6 syntax, and when you need to support older browsers or use the ES5 syntax, you can use the slice and reverse methods together. Let's look at it now.
How to use the Slice and Reverse methods to reverse arrays in JavaScript
The slice method is used to return the selected element as a new array, and when you call a method without any arguments, it returns the same new array as the original array (from the first element to the last element).
Next, you call the reverse method on the newly returned array, which is why the original array is not reversed:
Let numbers = [1,2,3,4,5]; let reversedNumbers = numbers.slice (). Reverse (); console.log (reversedNumbers); / / [5, 4, 3, 2, 1] console.log (numbers); / / [1, 2, 3, 4, 5]
How to reverse an array in JavaScript without a Reverse method
Sometimes the interview will challenge you to reverse the array instead of using the reverse method. No problem! You can use a combination of for loops and array push methods, such as the following example.
Let numbers = [1,2,3,4,5]; let reversedNumbers = []; for (let I = numbers.length-1; I > = 0; iMurray -) {reversedNumbers.push (numbers [I]);} console.log (reversedNumbers)
How to write your own inversion function with JS
Finally, suppose your task is to write your own inversion function, which needs to reverse the array without creating a copy. At first glance, this may seem complicated, but don't worry, because it's actually very simple.
What you need to do here is swap the first and last elements of the array, then swap the second and penultimate elements, and so on, until all the elements are swapped.
Let's write a function to do this.
Write the function customReverse and use array.length-1 as a variable, storing the first index 0 and the last index at the same time.
Function customReverse (originalArray) {let leftIndex = 0; let rightIndex = originalArray.length-1;}
Next, create a while loop that runs as long as leftIndex is less than rightIndex.
Within this loop, swap the values of leftIndex and rightIndex, and you can temporarily store one of the values in a temporary variable:
While (leftIndex < rightIndex) {/ / Exchange element let temp = originalArray [leftIndex]; originalArray [leftIndex] = originalArray [rightIndex]; originalArray [rightIndex] = temp;}
Finally, move leftIndex up, move rightIndex down, and when the while loop repeats, it swaps the penultimate element, and so on:
Function customReverse (originalArray) {let leftIndex = 0; let rightIndex = originalArray.length-1; while (leftIndex < rightIndex) {/ / swap elements let temp = originalArray [leftIndex] with temp variables; originalArray [leftIndex] = originalArray [rightIndex]; originalArray [rightIndex] = temp; / / move the index to the middle leftIndex++; rightIndex--;}}
When there are no other elements to reverse, the loop stops immediately. For odd arrays, the values of leftIndex and rightIndex will be equal, so there is no need to swap. LeftIndex will be greater than rightIndex for even-numbered arrays.
You can test this feature to see if it works properly, as shown below:
Let myArray = [1, 2, 3, 4, 5]; customReverse (myArray); console.log (myArray); / / output is [5, 4, 3, 2, 1] Thank you for reading, the above is the content of "JavaScript how to achieve array inversion operation". After the study of this article, I believe you have a deeper understanding of how to achieve array inversion operation in JavaScript, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.