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:
var array = [5,1,2,3,4];
and the result I want is:
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
// ONLINE-RUNNER:browser;
var array = [5, 1, 2, 3, 4];
array.push(array.shift());
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 comments
Add comment