You can use push()
, pop()
, shift()
and unshift()
methods:
function arrayRotate(arr, reverse) {
if (reverse) arr.unshift(arr.pop());
else arr.push(arr.shift());
return arr;
}
usage:
arrayRotate([1, 2, 3, 4, 5]); // [2, 3, 4, 5, 1];
arrayRotate([1, 2, 3, 4, 5], true); // [5, 1, 2, 3, 4];
If you need count
argument see my other answer:
https://stackoverflow.com/a/33451102 ๐ค๐งก๐๐๐