JS extracts n non-repeating data from an array

Source: Internet
Author: User

/*** 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.