赞
踩
<script> let arrInitial = [{ name: "王一", bh: 1 }, { name: "王二", bh: 2 }, { name: "王三", bh: 3 }, { name: "王四", bh: 4 }, { name: "王五", bh: 5 }, { name: "王六", bh: 6 }, { name: "王七", bh: 7 }, { name: "王八", bh: 8 }, { name: "王九", bh: 9 }, { name: "王十", bh: 10 } ]; function exportArr() { let arr = JSON.parse(JSON.stringify(arrInitial)); let finnalArr = []; // console.log(arr, '初始'); let randomNum = arrInitial.length - 2; //用来算随机数的 // 随机一个在数组里减掉一个,然后再循环,最后一个的时候直接push进去 function again() { if (randomNum == -2) { finnalArr.push(arr[0]); arr.splice(0, 1); } else { let everyNum = Math.round(Math.random() * randomNum + 1); randomNum--; finnalArr.push(arr[everyNum]); arr.splice(everyNum, 1); } } // 数组有几个循环几次 for (let i = 0; i < arrInitial.length; i++) { again() } console.log(arr, finnalArr); } exportArr(); </script> </body>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。