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
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).
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.
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.
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.
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).
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
Empty Array
Three ways:
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,