const shuffle = (arr) => { const list = [ ...arr ] list.sort(() => { return 0.5 - Math.random() }) return list } const a = [1, 2, 3, 4, 5, 6] console.info(shuffle(a)) console.info(a)
実行結果(例)
[ 4, 1, 5, 6, 3, 2 ] [ 1, 2, 3, 4, 5, 6 ]
牌語備忘録 -pygo
const shuffle = (arr) => { const list = [ ...arr ] list.sort(() => { return 0.5 - Math.random() }) return list } const a = [1, 2, 3, 4, 5, 6] console.info(shuffle(a)) console.info(a)
実行結果(例)
[ 4, 1, 5, 6, 3, 2 ] [ 1, 2, 3, 4, 5, 6 ]