albert105 albert105 - 1 year ago 73
Javascript Question

Rearrange array in Javascript with ES6

I want to reorder an Array with ES6.
For example:


When my starting number is 5, I want a new array like this:


I am able to fix this by looping, comparing, slicing, glueing the thing back together.

However I've read some interesting array functionality with ES6 that might make this easier. But I'm having trouble putting this into practise.

Answer Source

It can be

let newArr = [...arr.slice(arr.indexOf(5)), ...arr.slice(0, arr.indexOf(5))]


let newArr = [...arr];
newArr = [...newArr.splice(arr.indexOf(5)), ...newArr];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download