JS Tool Function Summary

Source: Internet
Author: User

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

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.