JavaScript arrays and mathematical objects

Creating an array:

       var ary1 = new new the Array (); 
       the console.log (ary1); // [] 

var ary2 = new new the Array ( 2 , . 3 , . 4 , . 5 ); the console.log (ary2); // [2,3,4, . 5] var ary3 = new new the array ( . 4 ); // pass a parameter for the number representing the length of the array the console.log (ary3); // [* empty. 4] ary3.length = . 8 ; // length attribute array may be reading can write console.log (ary3); // [empty * 8]

An array of methods:

push()

  • Features: Add item to the end of the array;
  • Parameters: add items into the array, one or more of;
  • Return value: new length number;
  • Change the original array

pop()

  • Function: Remove one at the end of the array;
  • Parameters: no;
  • Returns: delete the item;
  • Change the original array

unshift()

  • Function: Adds an item at the beginning of the array;
  • Parameters: add items into the array, one or more of;
  • Return value: new length number;
  • Change the original array

shift()

  • Function: Remove one at the beginning of the array;
  • Parameters: no;
  • Returns: delete the item;
  • Change the original array

splice()

  • Function: Extract the specified item in the array;
  • parameter:
            (n) taken from the index n to the start end of the array 
            (n, m), taken from the start index n m pieces of 
            parameters after the (n, m, x, y , z) m as an array from the array entry added to the extraction position
  • Return Value: intercepting a new array of items;
  • Change the original array

slice()

  • Function: Copy items from the array;
  • parameter:
    (N) from the copy start to the end of the array index n
    (N, m) from the start copying index n to the index m (not including m)
  • Returns: an array of items copied out of the composition;
  • Does not change the original array

join()

  • Function: the specified array connector, to string;
  • Parameters: connector, the default is a comma;
  • Return value: String;
  • Does not change the original array
      var str1 = ary.join('<span>'+val+'</span>');
      p.innerHTML = str1;

     

concat()

  • Function: the key array or arrays assembled into an array;
  • Parameters: single or array;
  • Return Value: spliced ​​array;
  • Does not change the original array

indexOf()

  • Function: Find an index in the array of values;
  • Parameters: The item to find;
  • Return Value: Index;
  • Does not change the original array

reveres()

  • Function: Descending array;
  • Parameters: no;
  • Return Value: reverse after the original array;
  • Change the original array

sort()

  • Features: Sorting an array;
  • Parameters: parameter does not pass, the default characters arranged in ascending order of encoding;
                Row of numbers: 
                function (A, B) { return A- B}; ascending 
                function (A, B) { return B- A}; descending 
                row letters: 
                function (A, B) { return a.localeCompare (B)} ; ascending 
                function (a, b) { return b.localeCompare (A)}; descending 
                row Chinese: 
                function (a, b) { return a.localeCompare (B, ' ZH ' )}; ascending 
                function (a, b) { return b.localeCompare (a, ' ZH ' )}; in descending order 
                according to each of an array of a sort attribute 
                function (a, b) {return a.name.localeCompare(b.name,'zh')};
                function(a,b){ return a.age-b.age};
  • Return Value: original sorted array;
  • Change the original array

Iterative method array:

        Array-based methods can not be used directly supplied array, the array need to be converted to an array type in order to use

 ```
            var  ary = [3,7,4,9];
            var res = ary.every(function(item,index){
                return item > 2;
            });
            console.log(res);  //true

var res = ary.some(function(item,index){ return item > 5 ; }) console.log(res); //true

var res = ary.filter(function(item,index){ return item%2 == . 1 ; }) the console.log (RES); // [3,7,9]
// forEach array traversal is simply to look var RES = ary.forEach (function (Item, index) { the console.log ( index + ' : ' + Item); }) the console.log (RES); // undefined var RES = ary.map (function (Item, index) { return Item + . 1 ; }) the console.log (RES); // [4, 8, 5, 10] `` `

 

Mathematical objects:

  • Math.abs () - absolute value;
  • Math.ceil () - rounding up;
  • Math.floor () - rounded down;
  • Math.min () - a minimum value;
  • Math.max () - maximum value;
  • Math.pow (n, m) - n m-th power;
  • Math.sqrt (m) - the square root of m;
  • Math.random () - Get 0-1 randomly float;
  • Obtaining a random number between nm
                function getRandom(n,m){
                    return parseInt(Math.random()*(m-n+1)+n);
                }

     

 

Guess you like

Origin www.cnblogs.com/musong-out/p/11421151.html