logo@krutoo/utils

shuffle

Randomly sorts given array and returns it. This function mutates given array.

import { shuffle } from '@krutoo/utils';

const array = [1, 2, 3, 4, 5, 6, 7];

// mutates given array
shuffle(array);

If you need to specify random() function you can use options argument:

shuffle(array, {
  random: myCustomRandomizer,
});

Why not immutable?

In modern JS you can simply make behavior immutable by spread syntax:

const list = [1, 2, 3, 4, 5];

const shuffled = shuffle([...list]);