How to shuffle an array in JavaScript

Publicado em: 24 Junho 2020
no canal de: Code With Bubb
14,526
264

In this tutorial, we'll look at how to shuffle an array in JavaScript.

🚀 Free JavaScript Essentials Course! 👉 https://codewithbubb.gumroad.com/l/ugyud
📖 Free 32-page eBook of JavaScript HowTos 👉 https://bit.ly/2ThXPL3

Follow me on Twitter:   / codebubb  
Facebook:   / juniordevelopercentral  
Blog: https://www.juniordevelopercentral.com/

If you have an array in JavaScript that has any type of data in it, you can shuffle up the position. of the items in the array in a couple of ways.

In the video, I will show you first of all the easies way to shuffle an array in JavaScript using a simple custom sort function. This works well for most situations but there is some evidence that this might not be entirely random.

So once we've looked at the basic array shuffle in JavaScript, we'll look at a recommended algorithm for shuffling arrays, the Fisher Yates Algorithm.

To do a Fisher Yates shuffle in JavaScript we simply loop through the array and swap out items at random positions. Although not too complicated, the Fischer Yates JavaScript implementation is a few more lines of code and not as succinct as using a custom sort function.

Either way is fine depending on your needs!

Check out some of the other Junior Developer Central JavaScript HowTos:    • How To Redirect A Page With JavaScript  

#JavaScript #JavaScriptHowtos #ArrayShuffle Channel Handle @codebubb


Nesta página do site você pode assistir ao vídeo on-line How to shuffle an array in JavaScript duração hora minuto segundo em boa qualidade , que foi baixado pelo usuário Code With Bubb 24 Junho 2020, compartilhe o link com seus amigos e conhecidos, no youtube este vídeo já foi visto 14,526 vezes e gostou 264 espectadores. Boa visualização!