# 如何把一个数组随机打乱
Issue
欢迎在 Issue 中交流与讨论: Issue 504 (opens new window)
Author
使用原生实现,Math.rondom() - 0.5 有时大于0,有时小于0会达成这样的效果
[1, 2, 3, 4].sort((x, y) => Math.random() - 0.5)
借用 lodash
可更方便
_.shuffle([1, 2, 3, 4])
//-> [3, 2, 4, 1]
Issue
欢迎在 Issue 中交流与讨论: Issue 504 (opens new window)
Author
使用原生实现,Math.rondom() - 0.5 有时大于0,有时小于0会达成这样的效果
[1, 2, 3, 4].sort((x, y) => Math.random() - 0.5)
借用 lodash
可更方便
_.shuffle([1, 2, 3, 4])
//-> [3, 2, 4, 1]