-
Creation method:
-
Array constructor
-
literal
-
Array Static method to create an Array
-
From: array. from(class Array object, mapping function (optional), this(optional))
Parameter 1: array-like object, that is, any structure that can be iterated over, or a structure with a length attribute and indexable elements
Parameter 2: An optional mapping function that directly enhances the value of the new array
Parameter 3: Specifies the value of this for the mapping function
//eg Array.from('Matt');//['M','a','t','t']; // Perform a shallow copy of an existing array const a1=[1.2.3.4]; const a2 = Array.from(a1);/ / a2 = [1, 2, 3, 4]; // The object with the length attribute const obj = { 0:1.1:2.length:2 } Array.from(obj);/ / [1, 2] // The second argument const a2 = Array.from(a1,function(x){return x*this.exponent},{exponent:2}); / / a2 =,4,6,8 [2] Copy the code
-
Of: an Array of () needs to be converted to an Array of parameters can be put into an Array, a set of parameters is used to replace es6 before common Array. The prototype. Slice. The call (the arguments) an Array parameter to a clumsy way
// eg Array.of(1.2.3.4) / / [1, 2, 3, 4] Copy the code
-
-
-
Common methods:
2.1 Changing the original array
-
Splice: splice(start index to delete, number of deletions,… Added data) – returns the deleted array or []
-
Shift: Shift () popup – returns popup data
-
Unshift: Unshift (inserted data) header insert – Returns the length of the array after insertion
-
Pop: Pop () – returns popup data
-
Push: push(inserted data) – Returns the length of the inserted array
-
Reverse: Reverse () reverses the array — returns a reference to the array that called the method
-
Sort: sort(callback) By default, the array is sorted according to the order of the string. Returns a reference to the array that called the method
-
Fill: Fill (value, start index (select), end index (select)) Insert all or part of the same value into the existing array
The negative index starts at the end of the array and can be changed to negative index + array length = true index,
Returns a reference to the array that called the method
-
CopyWithin: copyWithin(insert index position, start index copy (optional), end index copy (optional))
Copies a portion of the array in the specified range and inserts it at the beginning of the specified index
Returns a reference to the array that called the method
2.2 Do not change the original array
-
Slice: arr.slice(start index, end index) intercepts a segment of an array – returns the intercepted data
-
Concat: arr. Concat (arR2) Concatenated array – Returns the new array concatenated
-
IndexOf: indexOf(element searched, starting index searched (optional)) searches from left to right – returns the searched index or -1
-
LastIndexOf: lastIndexOf(element searched, starting index searched (optional)) searches from right to left – returns the searched index or -1
-
Includes: Includes (search element, search starting index (optional)). Search from left to right – returns a Boolean value
-
toString: toString(); Returns a comma-separated string concatenated from the equivalent strings of each value in the array
-
The valueOf: the valueOf (); Return the array itself
-
ToLocalString: toLocalString() returns a comma-separated string concatenated from the equivalent strings of each value in the array
-
Join: join(specify the join character); Returns a string delimited by the specified symbol concatenated equivalent string of each value in the array
-
Flat: flat(depth (select, default 1)); Loops through the array at Infinity depth and merges all traversed elements or subelements into a new array and returns, (note: Flat removes empty items from array)
-
flatMap: flatmap(callback(currentValue [, index [, array]]),thisArg);
Map each element using a mapping function, then compress the result into a new array and return the new array
2.3 Iterative methods (run the passed function on each item [], excluding elements removed from the array or never assigned)
-
Filter: Returns an array of items for which the function returns true
-
Some: Returns true if one of the functions executed returns true
-
Every: Returns true if every function returns true
-
Map: Returns an array of the results of each function call
-
ForEach: no return value
2.4 Iterator method: Used to retrieve the contents of an array
Keys: array.keys () returns an iterator for an Array index. - values: array.values () returns an iterator for an Array element. - Entries: array.entries () returns an iterator for an index/value pairCopy the code
2.5 Merging method
-
Reduce:
-
reduce(callback(accumulator,currentValue[, currentIndex[, array]])[,initialValue]);
Accumulator: Accumulator. The accumulated value returned during the last callback
CurrentValue: indicates the currentValue
CurrentIndex: indicates the currentIndex
Array: array for invoking Reduce
InitialValue: The value of the first argument of the function called for the first time. If not, the first element of the array is used. If the array is empty, an error is reported
-
-
ReduceRight: Same as above, except that reduce is executed from left to right, and reduceRight is executed from right to left
-