Skip to content

Instantly share code, notes, and snippets.

@userkang
Created January 21, 2019 10:42
Show Gist options
  • Save userkang/c7c6553a82e803156f0a22fbbee7ae11 to your computer and use it in GitHub Desktop.
Save userkang/c7c6553a82e803156f0a22fbbee7ae11 to your computer and use it in GitHub Desktop.
打乱数组
// ES5版本
function shuffle(a) {
var i,j,t
for(i = a.length; i; i--) {
j = Math.floor(Math.random() * i)
t = a[i-1]
a[i-1] = a[j]
a[j] = t
}
return a
}
// ES6版本
function shuffle(a) {
for(let i = a.length; i; i--) {
let j = Math.floor(Math.random() * i);
[a[i-1], a[j]] = [a[j], a[i-1]]
}
return a
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment