loading...

JavaScript ES6 one-liners: rotate an array

sergeis profile image Sergei ・1 min read
const arr = [1,2,3,4,5];
const shift = 2;

// rotate left by n < array length:
console.log(`rotated: ${[...arr.slice(shift), ...arr.slice(0,shift)]}`);
// rotated: 3,4,5,1,2

Discussion

pic
Editor guide