Make It Easy Togather
2021-02-08 09:55:00 +0000
I want swap values with javascript.
We often face to the situation where swapping values. For example, swapping contents in arrays. However, javascript does not prepare swap function basically. In this post, I introduce some solutions to swap values and suppose the situation we should swap values in the same array for simplicity. So, we want to do as follows.
let array = [1, 2, 3, 4, 5];
// Swap array[0] and array[4]
// Coding here!
console.log(array);
// Output -> [5, 2, 3, 4, 1]
Prepare escaping member such as temp
.
let temp = array[0];
array[0] = array[4];
array[4] = temp;
It is one of the basic solutions, but when we want to swap more than three values, this solution needs multiple lines for swapping processing like this…
let temp = array[0];
array[0] = array[2];
array[2] = array[4];
array[4] = temp;
On the other hand, the following solution is very simple.
[array[0], array[4]] = [array[4], array[0]];
In addition, this solution covers swapping more than three values like this…
[array[0], array[2], array[4]] = [array[2], array[4], array[0]];
We can code multiple-values-swapping with only one line.
By applying array matching, we can swap multiple values in one line.