This method returns a completely new array so it does not return a reference array like the shuffle function in Java.
Ideas are as follows:
1. Create a new function passing in an array that needs to scramble the subscript
2. Get the length of the array
3. Create a new array to save and return the result
4, according to the length of the array to create a new random number (random number remember to take down the whole or will subscript out of bounds)
5, Loop added to the result array inside
The code is as follows:
<script type= "Text/javascript" >varTestarr =NewArray ("China", "United States", "England", "France", "Germany", "Spain", "Greece",
"Italy", "Japan", "Korea", "Nigeria", "India"); varNEWARR =Shuffle (Testarr); Console.log (NEWARR); functionShuffle (arry) {varSortarr =NewArray (); varlen = arry.length;//get Array length specify random number of times for(vari = 0; I <Len;) { varindex = Math.floor (Math.random () *Len); if(Arry[index]! =NULL) {Sortarr.push (Arry[index]); I++; Arry[index]=NULL; } } returnSortarr; } </script>
JS "Practice" with JS wrapper Java Shuffle function (scrambled array subscript method)