1. Get random numbers within a specified range
function Getradomnum (Min,max) { return Math.floor (Math.random () * (Max-min + 1) + min;}
2. Randomly get the elements in the array
function Getradomfromarr (arr) { return Arr[math.floor (Math.random () *arr.length)];}
3. Generate a numeric array from 0 to a specified value
function GetArray (len) {var arr = [],i = 1;for (; Arr.push (i++) < Len;); Console.log (arr)}
Equivalent to:
function GetArray (len) {var arr = [],i = 1;for (; i < Len; i++) {Arr.push (i)}console.log (arr)}
4. Scramble the order of the array of numbers
var arr = [1, 2, 3, 4, 5, 6, 7, ' A ', ' Dsfs ', 8, 9, ' V '];arr.sort (function () {return math.random ()-0.5});
5. Object conversions to Arrays
Note that the object must be in the following format in order to be able to convert arrays//obtained DOM collections, and arguments of functions can also be converted 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 is set
function IsArray (obj) {return Object.prototype.toString.call (obj) = = = ' [Object Array] ';}
7. Get the maximum or minimum value in the array
function Maxandmin (arr) { return {max:Math.max.apply-null,arr.join (', ') . Split (', ')), min: Math.min.apply (Null,arr.join (', '). Split (', ')) }}
8. Empty the array
Way one by setting the length to 0var arr = [1, 2, 3, 4, 5];arr.length = 0;//Way Two by 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 is simply to reassign 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. Keep specified decimal digits
var num =4.345678;num = num.tofixed (4); 4.3457 the first four decimal digits are calculated by rounding
10. Generate a random alphanumeric string of the specified length
Generates a random alphanumeric string of the specified length */* notation a function getrandomstr (len) {var str = ""; Str.length < Len; str + = Math.random (). toString (+). substr (2)); return str.substr (0, Len);} Notation two function getrandomstr (len) {var str = ""; 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 occurrences of the element in the array, and give the position where it occurred.
function 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 Tips for JS development