Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shift array to right in javascript [duplicate]

Tags:

javascript

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...

like image 659
mike44 Avatar asked Apr 19 '13 04:04

mike44


People also ask

What does .shift do in JavaScript?

shift() The shift() method removes the first element from an array and returns that removed element.

Does JavaScript array allow duplicates?

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.

What does shift and Unshift do in JavaScript?

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.


3 Answers

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());
    }
}
like image 93
gilly3 Avatar answered Sep 28 '22 21:09

gilly3


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

like image 28
zerkms Avatar answered Sep 28 '22 21:09

zerkms


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));
like image 32
user1106925 Avatar answered Sep 28 '22 19:09

user1106925