22
common/resources/client/utils/array/shuffle-array.ts
Executable file
22
common/resources/client/utils/array/shuffle-array.ts
Executable file
@@ -0,0 +1,22 @@
|
||||
export function shuffleArray(items: any[], keepFirst = false) {
|
||||
let first = keepFirst ? items.shift() : null;
|
||||
|
||||
let currentIndex = items.length,
|
||||
temporaryValue,
|
||||
randomIndex;
|
||||
|
||||
while (0 !== currentIndex) {
|
||||
randomIndex = Math.floor(Math.random() * currentIndex);
|
||||
currentIndex -= 1;
|
||||
|
||||
temporaryValue = items[currentIndex];
|
||||
items[currentIndex] = items[randomIndex];
|
||||
items[randomIndex] = temporaryValue;
|
||||
}
|
||||
|
||||
if (first) {
|
||||
items.unshift(first);
|
||||
}
|
||||
|
||||
return [...items];
|
||||
}
|
||||
Reference in New Issue
Block a user