I have this array:
var arr1 = ['a1', 'a2', 'a3', 'a4', 'a5'];
I need to shift it to right by say 2 locations, like
arr1 = ['a4', 'a5', 'a1', 'a2', 'a3'];
This works for left shift:
arr1 = arr1.concat(arr1.splice(0,2)); // shift left by 2
I get :
arr1 = ['a3', 'a4', 'a5', 'a1', 'a2'];
But I don't know how to do shift right...
shift() The shift() method removes the first element from an array and returns that removed element.
Using Array.If any item in the array, both indices don't match, you can say that the array contains duplicates. The following code example shows how to implement this using JavaScript some() method, along with indexOf() and lastIndexOf() method. Output: Duplicate elements found.
The shift() method in JavaScript removes an item from the beginning of an array and shifts every other item to the previous index, whereas the unshift() method adds an item to the beginning of an array while shifting every other item to the next index.
You can use .pop()
to get the last item and .unshift()
to put it at the front:
function shiftArrayToRight(arr, places) {
for (var i = 0; i < places; i++) {
arr.unshift(arr.pop());
}
}
Shift to right to N
positions == shift to left to array.length - N
positions.
So to shift right on 2 positions for you array - just shift it left on 3 positions.
There is no reason to implement another function as soon as you already have one. Plus solutions with shift/unshift/pop
in a loop barely will be as efficient as splice + concat
Using .concat()
you're building a new Array, and replacing the old one. The problem with that is that if you have other references to the Array, they won't be updated, so they'll still hold the unshifted version.
To do a right shift, and actually mutate the existing Array, you can do this:
arr1.unshift.apply(arr1, arr1.splice(3,2));
The unshift()
method is variadic, and .apply
lets you pass multiple arguments stored in an Array-like collection.
So the .splice()
mutates the Array by removing the last two, and returning them, and .unshift()
mutates the Array by adding the ones returned by .splice()
to the beginning.
The left shift would be rewritten similar to the right shift, since .push()
is also variadic:
arr1.push.apply(arr1, arr1.splice(0,2));
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