/*** First, do a deduplication for this array, so that you don't have to fetch the data at the back, because you take the same element and you have to fetch the random number again .* will get to the non-repeating array, and then to the sample inside to fetch random numbers* Remove this element from the "non-repeating array" Every time it is taken, so that the length will be reduced by 1* Finally returns the array that we have taken, which is the data that is not duplicated*///array passed in--go to weightfunction Unique(array){ LetArr=[]; for( LetIinchArray{ if(arr.indexOf(Array[i]< 0)){ arr.Push(Array[i]); } } returnArr;}//Filter The specified number of datafunction Filterdata(Origin,Number{ //1. First the data deduplication, if not duplicated, skip this step LetUniquearr= Unique(origin); ///2, number must be less than or equal to the number of the array after the re- if(number>Uniquearr){ return; } LetArr=[]; LetRandom,TempData; for( LetI=0;I<Number;I++){ //Generate random numbersRandom= parseint(Math.Random()*(Uniquearr.length-1)); //The selected individual data is stripped from the Uniquearr sample, so length will gradually decreaseTempData= Uniquearr.Splice(Uniquearr[random],1); //Because TempData is an array of length 1, it is possible that the data inside is a string, a number, an object, an array //Note, do not write directly to Arr.push (TempData); Because of you, the arr that you produce is actually an array of nested arrays, like this [["A"],["B"] ... . arr.Push(tempdata[0]); } returnArr;}//RAW datavarOrigindata=[1,2,3,4,5,6,7,8,"9AA","SDD","Aaq","QQ","WW","E","R","T","Y","U","I","V","B","n","J","K","I",{"a":1},{"B":2}];Console.Log(Filterdata(Origindata,6));
JS extracts n non-repeating data from an array