From e686689b5243cf9f5f7de226e8ce7121aa528ea4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Quentin=20Hocde=CC=81?= Date: Wed, 25 Nov 2020 15:10:53 -0500 Subject: [PATCH] Add shuffle function in array utils --- assets/scripts/utils/array.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/assets/scripts/utils/array.js b/assets/scripts/utils/array.js index fe05bf7..36e9da7 100644 --- a/assets/scripts/utils/array.js +++ b/assets/scripts/utils/array.js @@ -86,3 +86,16 @@ export function findByKeyValue( array, key, value ) { export function cloneArray( array ) { return JSON.parse(JSON.stringify(array)); } + +/** + * Shuffles array in place. ES6 version + * @param {Array} a items An array containing the items. + */ +export function shuffle(a) { + for (let i = a.length - 1; i > 0; i--) { + const j = Math.floor(Math.random() * (i + 1)); + [a[i], a[j]] = [a[j], a[i]]; + } + return a; +} +