Shuffle an array

Published on
1 mins read
––– views

Simple array shuffling function

let shuffle = (arr) => arr.sort(() => 0.5 - Math.random())

Usage

shuffle([1, 2, 3, 4, 5])
// [3, 2, 5, 4, 1]
shuffle([1, 2, 3, 4, 5])
// [3, 1, 5, 4, 2]
shuffle(["foo", "bar", "buzz"])
// ['buzz', 'bar', 'foo']