A JS code that randomly takes an x without repeating data from an array

Source: Internet
Author: User

  This article mainly introduces the random x data from the array of the JS code, the need for friends can refer to the

Some operations on the array are frequently encountered in the work     1. Random x data from the data (PS: The following s.each is the Kissy.each method, you can change to a for loop)   code as follows:/* random x from the array arr of data not duplicated  */ function Myrand (arr,num) {    var newArr = [];     rand (num);    //random x       FUNCTI On rand (k) {        if (k==0) {            return;       & nbsp }         var index = Math.floor (Math.random () * arr.length);         var flag = true;         S.each (newarr,function (v) {            if (v = = Arr[index]) {&nbs P               flag = false;            }        }         if (flag) {            Newarr.push (Arr[index])     &NB Sp       k--;        }        Rand (k);    }     return NEWARR; &NBSP}       2. Random x data from object     code as follows:/* Randomly take x  */function Myrand () {  &nbsp from Object obj ; var S = Kissy;     var obj={     : {name: ' A '},       ': {name: ' B '},   & nbsp     ': {name: ' C '},         ': {name: ' d '},       ': {name: ' E '}, & nbsp       ' a ': {name: ' F '},       '   ': {name: ' G '},       ': {name: ' H '},         ': {name: ' I '},       ': {name: ' G '}    };     var arr = [];     S.each (obj,function (v,k) {        Arr.push (k);    });    //randomly take 3     var newArr = Myrand (arr,3);     S.each (newarr,function (b) {        Console.log (obj[b]);    })};     3. Remove duplicates of array   code as follows:/* Remove duplicate of array  */function Uniqarr (arr) {    function toobject (a) {    &nbs P   var o = {};         for (Var i=0, j=a.length i<j; i=i+1) {          O[a[i]] = True        }         return o;    };     function keys (o) {        var a=[], I;         (i in O) {&nbsp ;           if (O.hasownproperty (i)) {//Here, Yui Source is Lang.hasownproperty (o, i)                 A.push (i);             {       }         return A;    };     Return keys (Toobject (arr)); }

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.