Skip to content

Instantly share code, notes, and snippets.

@laispace
Created August 31, 2015 12:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save laispace/59179ca4f1644cca856f to your computer and use it in GitHub Desktop.
Save laispace/59179ca4f1644cca856f to your computer and use it in GitHub Desktop.
打乱数组顺序
function randomArray (array) {
if (array.length === 1) {
return array;
} else {
var len = array.length;
var index = ~~(Math.random() * len);
var item = array.splice(index, 1);
return item.concat(randomArray(array));
}
}
var array = [1,2,3,4,5,6];
var result = randomArray(array);
console.log(result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment