随机数组值的顺序

5年以前  |  阅读数:380 次  |  编程语言:JavaScript 

随机数组值的顺序。

使用Array.sort()可在比较器中使用Math.random()重新排序元素。

const shuffle = arr => arr.sort(() => Math.random() - 0.5);
// shuffle([1,2,3]) -> [2,3,1]

Copyright© 2013-2020

All Rights Reserved 京ICP备2023019179号-8