美文网首页
数组随机排列

数组随机排列

作者: 萤火虫de梦 | 来源:发表于2018-08-10 22:40 被阅读23次

es6 方法

function shuffle(arr) { 
  let i = arr.length; 
 while (i) { 
 let j = Math.floor(Math.random() * i--); 
 
        [arr[j], arr[i]] = [arr[i], arr[j]]; 
 
    } 

es5 方法

function shuffle(arr) { 
 
  var i = arr.length, t, j; 
 
  while (i) { 
 
    j = Math.floor(Math.random() * i--); 
 
    t = arr[i]; 
 
    arr[i] = arr[j]; 
 
    arr[j] = t; 
 
  } 
 
}

相关文章

网友评论

      本文标题:数组随机排列

      本文链接:https://www.haomeiwen.com/subject/pyqrbftx.html