javascript array move element one position

Solutions on MaxInterview for javascript array move element one position by the best coders in the world

showing results for - "javascript array move element one position"
Marta
15 Mar 2016
1// Move element '3' to where currently '1' is
2const numbers = [ 1, 2, 3, 4, 5 ]
3const numbersOriginal = Object.assign(numbers)
4const sourceIndex = 2
5const targetIndex = 0
6numbers.splice(targetIndex, 0, numbers.splice(sourceIndex, 1)[0])
7console.log(numbersOriginal)
8console.log(numbers)