What is the best way to move the first element of the array till the end of this same array?
ie: [a,b,c,d]
"Some operation"
result: [b,c,d,a]
What should this "Some operation" be?
To change the position of an element in an array:Use the splice() method to insert the element at the new index in the array. The splice method changes the original array by removing or replacing existing elements, or adding new elements at a specific index.
1) Using the array length property The length property returns the number of elements in an array. Subtracting 1 from the length of an array gives the index of the last element of an array using which the last element can be accessed.
There is Array#rotate
:
[a,b,c,d].rotate(1)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With