EN
JavaScript - what is the fastest way to move first element to the end of array?
1 answers
0 points
What is the fastest way to move an element from the beginning of an array to the end in JavaScript?
For example, if we have:
xxxxxxxxxx
1
var array = [5,1,2,3,4];
and the result I want is:
xxxxxxxxxx
1
var array = [1,2,3,4,5];
1 answer
0 points
Just combine shift()
and push()
methods:
-
shift()
method removes the first element from an array and returns that removed element, -
push()
method adds the element at the end of the array.
Practical example
xxxxxxxxxx
1
var array = [5, 1, 2, 3, 4];
2
3
array.push(array.shift());
4
5
console.log(array); // [ 1, 2, 3, 4, 5 ]
Note:
If you want to move any element to the first/last position check out the See also section below.
See also
References
0 commentsShow commentsAdd comment