Returns a shuffled copy of the input array.
Type of array items
Input array
Random generator. Math.random by default.
Math.random
Copy with items moved around randomly
const d = [1, 2, 3, 4];const s = shuffle(d);// d: [1, 2, 3, 4], s: [3, 1, 2, 4] Copy
const d = [1, 2, 3, 4];const s = shuffle(d);// d: [1, 2, 3, 4], s: [3, 1, 2, 4]
It can be useful to randomly access each item from an array exactly once:
for (const value of shuffle(inputArray)) { // Do something with the value...} Copy
for (const value of shuffle(inputArray)) { // Do something with the value...}
Returns a shuffled copy of the input array.