Useful array utilities

Published on
1 mins read
––– views
Shuffling an array
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']