The array to shuffle.
A new array with elements shuffled.
This function uses the Fisher–Yates shuffle algorithm to randomly reorder the elements of the input array. It is efficient and ensures that each permutation of the array is equally likely.
// ES Module
import { shuffleArray } from '@bnidev/js-utils'
// CommonJS
const { shuffleArray } = require('@bnidev/js-utils')
// Shuffle an array of numbers
const numbers = [1, 2, 3, 4, 5];
const shuffledNumbers = shuffleArray(numbers);
// Shuffle an array of strings
const strings = ['apple', 'banana', 'cherry'];
const shuffledStrings = shuffleArray(strings);
// Shuffle an array with undefined values
const mixedArray = [1, undefined, 2, undefined, 3];
const shuffledMixedArray = shuffleArray(mixedArray);
Returns a new array with the elements shuffled using the Fisher–Yates algorithm.