Quoted http://es6.ruanyifeng.com/#docs/array
1. Extension operators (...) )
The extension operator (spread) is a three-point ( ...
). It is like the inverse of the rest parameter, converting an array to a comma-delimited sequence of arguments.
This operator is primarily used for function calls.
function push (array, ... items) { Array.push (... items);} function add (x, y) { return x + y;} var numbers = [4//
Apply method to replace array
Because the extension operator can expand the array, the method is no longer needed apply
and the array is converted to the function's arguments.
//the writing of ES5function f (x, Y, z) {// ...}varargs = [0,1,2];f.apply (NULL, args);//the writing of ES6function f (x, Y, z) {// ...}varargs = [0,1,2];f (... args);
Application of extension operators
(1) Merging arrays
// merged arrays of ES5 Arr1.concat (ARR2, ARR3); // [' A ', ' B ', ' C ', ' d ', ' e '] // merged arrays of ES6 [... arr1, ... arr2, ... arr3] // [' A ', ' B ', ' C ', ' d ', ' e ']
(2) combined with deconstructed assignment '
// ES5a = list[0], rest = List.slice (1)// ES6[A, ... Rest] = List
(3) string
Correctly identifies 32-bit Unicode characters.
' x\ud83d\ude80y ' // 4 [... ' x\ud83d\ude80y ' // 3
(4) Map and Set structure, Generator function
Let map = new map ([[ 1 , " one " ", [ 2 , " two " ", [ 3 , " Span style= "COLOR: #800000" >three " ]); Let arr = [... Map.keys ()]; // [1, 2, 3]
var go = function*() { yield1; yield 2 ; yield 3 // [1, 2, 3]
2.array.from ()
Array.from
The Array-like method is used to convert two types of objects to a true array: An array-like object, and
Objects that can traverse (iterable) (including ES6 new data structure set and map).
Let Arraylike = { '0':'a', '1':'b', '2':'C', Length:3};//the writing of ES5varARR1 = [].slice.call (arraylike);//[' A ', ' B ', ' C ']//the writing of ES6Let arr2 = Array. from(Arraylike);//[' A ', ' B ', ' C ']
Array.from
You can also accept the second parameter, which acts like an array map
, to process each element and put the processed value into the returned array.
Array. from (arraylike, x = x * x); // equivalent to Array. from (arraylike). Map (x = x * x ); Array. from ([123], (x) = x * x)
3.array.of ()
Array.of
method is used to convert a set of values into an array.
Array.of (3each 8// [3,11,8]array.of (3// [3] Array.of (3// 1
Array.of
Can basically be used to replace Array()
or new Array()
, and there are no overloads caused by different parameters. Its behavior is very uniform.
4. Copywithin () of an array instance
0 this. Length)
It accepts three parameters.
- Target (required): Replace data from this location.
- Start (optional): Reads data from this location, which defaults to 0. If negative, the countdown is indicated.
- End (optional): Stops reading data before it is reached, which is equal to the array length by default. If negative, the countdown is indicated.
[12345].copywithin (03)// [ 4, 5, 3, 4, 5]
5. Find () and FindIndex () for array instances
A method of an array instance that is find
used to find the first qualifying array member. Its argument is a callback function, and all array members execute the callback function sequentially,
Until you find the first member that returns a value true
, then return the member. If there are no eligible members, the returnundefined
[151015]. Find (function ( Value{return value > 9; }//
The method of an array instance is findIndex
find
very similar to the method that returns the position of the first qualifying array member, or returns if all members do not meet the criteria -1
.
6. Fill () of the array instance
fill
Method fills an array with the given value.
['a'b'c'].fill (7 )// [7, 7, 7]new Array (3). Fill (7) // [7, 7, 7]
7. Entries () of the array instance, keys () and values ()
for(Let index of ['a','b'].keys ()) {Console.log (index);}//0//1 for(Let Elem of ['a','b'].values ()) {Console.log (elem);}//' A '//' B ' for(Let [index, Elem] of ['a','b'].entries ()) {Console.log (index, elem);}//0 "a"//1 "B"
8. Includes () of an array instance
Array.prototype.includes
Method returns a Boolean value that indicates whether a number group contains the given value, similar to the method of a string includes
[123].includes (2) // true[12 3].includes (4) // false[12// True
The second parameter of the method represents the starting position of the search, which defaults to 0
. If the second argument is a negative number, it indicates the position of the reciprocal,
indexOf
Method has two shortcomings, one is not enough semantics, its meaning is to find the first occurrence of parameter values, so to compare whether it is not equal -1
, the expression is not intuitive.
Second, it uses the strict equality operator ( ===
) to judge internally, which results in NaN
false positives.[NaN].indexOf(NaN)
The main knowledge point of ES6 (vi) array