Files
30-seconds-of-code/snippets/shuffle-array.md
2017-12-14 11:10:47 +02:00

9 lines
202 B
Markdown

### Shuffle array
Use `Array.sort()` to reorder elements, using `Math.random()` in the comparator.
```js
const shuffle = arr => arr.sort(() => Math.random() - 0.5);
// shuffle([1,2,3]) -> [2,3,1]
```