Common techniques in JS development

Source: Internet
Author: User

From: http://www.cnblogs.com/jone-chen/p/5897829.html
1, get the random number within the specified range 123function getradomnum (min,max) {return Math.floor (Math.random () * (Max-min + 1)) + min;} 2. Randomly get the elements in the array 1234function Getradomfromarr (arr) {return Arr[math.floor (Math.random () *arr.length)];} 3. Generate a numeric array from 0 to a specified value 12345function GetArray (len) {var arr = [],i = 1; for (; Arr.push (i++) < Len;); Console.log (arr)} is equivalent to: 1234567function GetArray (len) {var arr = [],i = 1; for (; i < Len; i++) {Arr.push (i)} console.log (arr)}4, disrupting the order of the array of numbers 1234var arr = [1, 2, 3, 4, 5, 6, 7, ' a ', ' Dsfs ', 8, 9, ' V '];arr.sort (function () {return math.random ()-0.5}), 5, object conversion to an array 1234567891011121314//note that the object must be in the following format in order to pass this The way to convert an array//Gets the DOM collection, and the arguments of the function can also be converted in this way to the array var obj = {0: ' Qian ', 1: ' Long ', 2: ' Chu ', 3: ' Tian ', length: 4}var Objarr = Array.prototype.slice.call (obj);//var Objarr = [].slice.call (obj);//var Objarr = Array.prototype.slice. Apply (obj); Console.log (Objarr) 6, verify that the array 123function IsArray (obj) {return Object.prototype.toString.call (obj) == = ' [Object Array] ';} 7. Gets the maximum or minimum value of 123456function maxandmin (arr) {return {max:Math.max.apply (', ') in the array. Split (', ')), M In:Math.min.apply (' Null,arr.join '). Split (', '))}}8, empty array 1234567891011//by setting the length to 0var arr = [1, 2, 3, 4, 5];arr.leng th = 0; Way two through the splice method var arr = [1, 2, 3, 4, 5];arr.splice (0, arr.length); Way three by assigning an empty array [] to an array (strictly speaking, this simply assigns the ary to an empty array, before the array if there is no reference in point to it will wait for garbage collection.) ) var arr = [1, 2, 3, 4, 5];arr = [];9, reserved for the specified decimal digit 12var num =4.345678;num = num.tofixed (4); 4.3457 the first four decimal digits are rounded to calculate 10, generate a random alphanumeric string of the specified length 1234567891011121314151617181920//generate a random alphanumeric string of the specified length */* notation a function Getrandomstr (len) {var str = ""; for (; str.length < len; str + math.random (). toString (. substr (2)); Return str.substr (0, Len);} Notation two function getrandomstr (len) {var str = ""; for (; str.length < Len;) {str + = Math.random (). toString (+). substr (2); } return str.substr (0, Len);} *//* three */function getrandomstr (len) {for (var str = '; str.length < Len; StR + = Math.random (). toString (+). substr (2)); Return str.substr (0, Len)}11, find the most frequently occurring element in the array, and give the position where it occurred 123456789101112131415161718192021222324252627function Getmaxandindex (arr) {var obj = {}; Arr.foreach (function (item, index) {if (!obj[item]) {Obj[item] = {indexs: [index] }} else {obj[item][' indexs '].push (index); } }); var num = 0; Record number of occurrences max var str = '; Record the most occurrences of the character Var Rearr; Returns the maximum value of the position array for (Var attr in obj) {var temp = obj[attr][' indexs '); if (Temp.length > num) {num = temp.length; str = attr; Rearr = temp; }} return {maxstr:str, Indexs:rearr}}

  

Common techniques in JS development

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.