Javascript

How To Shuffle An Array In Javascript

Posted by Weston Ganger on January 01, 2016

Creating a shuffle function for arrays can be complicated.

Here’s a 4 liner function for you that makes it really easy.


function shuffle(o){
  for(var j, x, i = o.length; i; j = Math.floor(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
  return o;
}

shuffle(myArray);


Related External Links:


Posted in Javascript and Tagged with array shuffle javascript 


Need help on your next project or application?

I specialize in Ruby-on-Rails, AngularJS, Javascript, Bootstrap, and Hybrid Mobile Apps with Cordova & Ionic.

Contact Me

Recommended Posts