The code copy is as follows:
<script language="javascript">
// From a given array arr, randomly return num without duplicates
function getArrayItems(arr, num) {
//Create a new array and copy the passed array for operation, instead of directly operating the passed array;
var temp_array = new Array();
for (var index in arr) {
temp_array.push(arr[index]);
}
//The extracted numerical items are saved in this array
var return_array = new Array();
for (var i = 0; i<num; i++) {
//Judge if the array has elements that can be retrieved, in case the subscript crosses the bounds
if (temp_array.length>0) {
//Create a random index in the array
var arrIndex = Math.floor(Math.random()*temp_array.length);
//Copy the corresponding array element value of this random index
return_array[i] = temp_array[arrIndex];
//Then delete the array element of this index, and at this time temp_array becomes a new array
temp_array.splice(arrIndex, 1);
} else {
//After the data items in the array are taken out of the loop. For example, the array originally had only 10 items, but it is required to take out 20 items.
break;
}
}
return return_array;
}
//test
var ArrList=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33];
alert(getArrayItems(ArrList,6));
</script>