Shuffle Array

Randomly shuffle the elements of an array (Fisher-Yates algorithm).

Code

General
const array = [...arr];
for (let i = array.length - 1; i > 0; i--) {
  const j = Math.floor(Math.random() * (i + 1));
  [array[i], array[j]] = [array[j], array[i]];
}
return array;

Parameters

Array to shuffle

Browser·fetch() may be limited by CORS

More JavaScript Snippets