JS data structure - an array of conventional methods Array array declaration

Array

  An identifier, storing a plurality of different data types.

Declare an array

  Format 1:

    Literal mode (recommended)
    Image Identifier = [data];
  Example let i = [1, "A ", undefined];
  
  in brackets the data delimiter between each of the comma, each data element is called ;
  each element has its own unique index (subscript), can be obtained by indexing the array elements in the array.

NOTE: The start value of the array index 0;
  I [. 1] to obtain an array of two elements "A".
  Obtaining a non-existent index will be undefined without error.

  Format 2:

    Constructor embodiment
    keyword identifier = new Array ();
  Example the Array new new = I the let ();
  the Array () parentheses length which can be given array, but no sense, because JS array is variable length, may be added at random;
  data can also be written directly Array (1, "A", undefined), but ... anyway recommended literal way.

  

 

 

 

  Literal distinction embodiment constructor

    Literal parsing speed faster than the constructor (high efficiency).
    the reason:
      Literal belongs JSON format, it can be directly parsed JS engine.
      Constructor you'll need to call the constructor, then JS engine parses.
 

ES6 array of deconstruction 

  Converting the reference data to the base data type;

  ES6 allowed according to a certain pattern, and the object is extracted from an array of values, variable assignment, which is referred to deconstructed (Destructuring).

  

  

 

 

Note: Strictly speaking right side content to play through, can be enumerated, such as an object, or a function of the parameter string, can be deconstructed.
 

Iterate

  method one

    

    Where i corresponds to the array index.

   Second way

    

 

     Where i is the value of each item array.

  Three ways

    

 

     for in itself is actually used to get the next subject, but can also be acquired for each such value;

    Get index is the direct output i, is worth noting that the returned value is a string type.

Property array

  On a common properties: length; length property is provided to change the size of the array may be.

  If the value is smaller than its current value, the array will be truncated tail of its elements will be lost.

  If the value is larger than its current value, the array increases, new elements are added to the end of the array, which is undefined.

  Note: If Three ways to iterate, not the value of the new element (undefined), seemingly no direct assignment will not get,

    Mode and a second approach can be obtained.

An array of methods

Add elements

  push () added after the array: let x = arr.push ( "F67", 345,3000); // you can add a plurality of values

  the unshift () is added in front of the array: let y = arr.unshift ( "F54", 110,500);

  NOTE: The return value is added after the data length of the array.

   concat () to create a copy of the original array, add elements on the basis of a copy of the return to a new array does not change the content of the original array.

  

 

 

 

Removing elements

  

 

Get bulk data array

  When the slice (parameter 1, parameter 2) The condition is not satisfied, it returns an empty array.

  1 is to obtain an array of parameters starting position, parameter 2 is to obtain an end position of the array;
  Taken content data, not the end position, i.e., from the parameter acquisition array 1 began, the contents of the parameters between the former two.
   Features:
    1, does not change the content of the original array
    2, if a parameter is negative, then adding thereto the length of the array.
    3. If not, it returns an empty array
    4, if a parameter is acquired from the location of the contents of the last array.
    

 

 

Delete, modify, insert

  splice () returns the contents removed.

  Directly change the original array.

  delete:

    Only one parameter is to start from the position of this parameter, to delete the final contents of the array.

    Two parameters, the parameters of 1: starting position, 2 parameters: the number that begins parameters 1, the next number of content removal.
    

 

  Modify or insert:
    Three parameters, one parameter: the starting position, two parameters: the number of parameters 3: modified value, i.e., 1 to replace the contents of the parameter to the value of parameter 2 3 parameter.
    Three parameters, one parameter: the starting position, parameter 2: 0, 3 parameters: modified value, i.e. the value of the parameter is inserted at the front position 3 of the parameters.
 
    Over three parameters, one parameter: the starting position, two parameters: the number 0 or,
    Parameter 3: insertion or modification values ​​..., parameter n: inserted or modified values ​​from the start of the three parameters, values ​​are to be inserted or modified.
    The parameter value of the parameter n is 3 to turn to a position to cover the original parameter array 1 on the back removed, the number of puncturing cover many, many add up.
      

   Note: The content did not meet the criteria or delete 0 (parameter 2 to 0), returns an empty array.

 

And conversion of the string array

  Turn an array of strings

    

 

 

  Array to String

    

 

Extended operator

  ... three points: Extended operator

  

 

 

Find Content

Find content index return

  indexOf () from front to back looking for first met.

  lastIndexOf () from the back to find the first meet.

Note: If not satisfied (i.e., content that does not exist in the array) -1.
 

Determine whether there is what to look for

  Returns true indexOf () the presence, absence returns false.

Determining whether a duplicate values ​​array

  method one:

      

 

  Second way:

      

 

 

In reverse order of the array

  It will change the original contents of the array:

    arr.reverse (); // returns an array of new reverse after the

 

Ascending and descending array (number type)

   Array itself sort () sort method, this method is invoked with no parameters, alphabetically elements in the array sequentially and character encoding.

   It is necessary to element of the array into a string. If you want to sort by other criteria, to provide a comparison function itself.

  

 

 

 

replace

  copyWithin () the designated element, corresponding to the replacement position (may change the content of the original array).

  Parameter 1: replacing from this position
  Parameter 2: Get the starting position of the specified element
  Parameter 3: Get the end position of the specified element (with no content end)
  Note: If you do not give parameter 3, parameter 2 is the end all are selected.
  

 

 

Data filling

  fill () the specified data, is filled into the corresponding location (change of the contents of the original array).

  Parameter 1: padded data

  Parameter 2: filling a starting position

  Parameter 3: filling end position (the end not included)
  Note: If you do not give parameter 3, parameter 2 is the end all are selected.
  

 

Empty Array

  Three ways:

    arr.splice(0);
    arr.length = 0;
    arr = [];

   You can get an empty array, but an array of empty this act itself, is really a bit redundant.

 

Determining whether there is an array NaN

  Because NaN is special mention,

  arr.includes (NaN); // return correctly if there is

 

Guess you like

Origin www.cnblogs.com/jiayouba/p/11921020.html