Note: http://phpjs.org/this site to PHP commonly used method with JS implementation, recommend
1. Randomly get several distinct items from the array
//randomly returns num non-duplicates from a given array of arr functiongetarrayitems (arr, num) {//Create a new array, copy the incoming array, and use it for the operation instead of manipulating the incoming array directly; varTemp_array =NewArray (); for(varIndexincharr) {Temp_array.push (Arr[index]); } //The value item that is taken out, saved in this array varReturn_array =NewArray (); for(vari = 0; i<num; i++) { //determine if the array has elements that can be removed to prevent the subscript from crossing if(temp_array.length>0) { //produces a random index in an array varArrindex = Math.floor (Math.random () *temp_array.length); //copies the corresponding array element values for this random indexReturn_array[i] =Temp_array[arrindex]; //then delete the array element of this index, when Temp_array becomes the new arrayTemp_array.splice (Arrindex, 1); } Else { //after the data items in the array have been fetched, exit the loop, such as an array that has only 10 items, but requires 20 items to be removed. Break; } } returnReturn_array; }
JS Tool Function Summary