I want to shuffle an array in the typescript
images:Symbols[] =
[this.seven,this.bell,this.watermelon,this.plum,this.lemon,this.cherry];
how can i shuffle it? Here images is the name of my array and symbols is the data type of the array.
I want to shuffle an array in the typescript
images:Symbols[] =
[this.seven,this.bell,this.watermelon,this.plum,this.lemon,this.cherry];
how can i shuffle it? Here images is the name of my array and symbols is the data type of the array.
Similar to as javascript
array.
But you would need to specify the types of the parameters and also a return type. Yes you could configure your setup to not care about these things, but then that would defeat the object of using typescript.
export function shuffle<T>(array: T[]): T[] {
let currentIndex = array.length, randomIndex;
// While there remain elements to shuffle.
while (currentIndex != 0) {
// Pick a remaining element.
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex--;
// And swap it with the current element.
[array[currentIndex], array[randomIndex]] = [
array[randomIndex], array[currentIndex]];
}
return array;
};
You can get different options of shuffle
from this question.