javascript 随机数组元素ES2015,ES6

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript 随机数组元素ES2015,ES6相关的知识,希望对你有一定的参考价值。

// original gist
const shuffleArray = arr => arr.sort(() => Math.random() - 0.5);

// fully random by @BetonMAN
const shuffleArray = arr => arr
  .map(a => [Math.random(), a])
  .sort((a, b) => a[0] - b[0])
  .map(a => a[1]);

shuffleArray([1, 2, 3]) //[3, 1, 2]

以上是关于javascript 随机数组元素ES2015,ES6的主要内容,如果未能解决你的问题,请参考以下文章