1. Extension operators
meaning : An extension operator is three dots (...). ) to convert an array to a comma-delimited sequence of arguments
usage : ... [' A ', ' B ', ' C ']
Console.log (... [1, 2, 3]) // 1 2 3 Console.log (1, 2, ... [3, 4, 5], 6) // 1 2 3 4 5 6Console.log (1, 2, ...) [3, 4, 5]) // 1 2 3) 4 5
If the extension operator is followed by an empty array, it does not produce a function
Console.log (1, ... []) // 1
Application of extension operators
(1), replace the array of the Apply method
A, the argument to the function of the array
// the writing of ES5 function Fun (x, Y, z) { console.log (x+y+z)}var arg = [1, 2, 3]fun.apply (null, a RG) /// 6// es6 notation function fun (x, Y, z) { Console.log (x+y+z)}var arg = [1, 2, 3]fun (... arg) // 6
b, take the maximum value of the array
// the writing of ES5 null, [3, 333]) // 333// ES6 the notation math.max (... [3, 333]) // 333
C, add an array to the end of another array
// the writing of ES5 var arr1 = [1, 2, 3]var arr2 = [4, 5, 6]array.prototype.push.apply (arr1, arr2) Console.log ( ARR1) // [1, 2, 3, 4, 5, 6]// es6 var arr1 = [1, 2, 3]
var arr2 = [4, 5, 6]arr1.push (... arr2) console.log (arr1) // [1, 2, 3, 4, 5 , 6]
D, another case provided by the official website
// ES5 New (Date.bind.apply (Date, [null, 1, 1])) // Sun Feb 00:00:00 gmt+0800 (China Standard Time) // ES6 New Date (...). [1, 1]) // Sun Feb 00:00:00 gmt+0800 (China Standard Time)
(2), merging arrays
ES5 connects two or more arrays using the contact () and returns a copy of the connected array
// ES5 var arr1 = [1, 2]var arr2 = [3, 4]var arr = arr1.concat (arr2) Console.log (arr) // [1, 2, 3, 4]
ES6 can use the extension operator to stitch [... arr1, ... arr2, ... arr3]
// ES6 var arr1 = [1, 2]var arr2 = [3, 4]var arr = [... arr1, ... arr2]console.log (arr) // [1, 2, 3, 4]
(3), string to array
The extension operator can also convert a string into a real array
[... ' Hello '] // ["H", "E", "L", "L", "O"]
(4), return value of function
JS's function can only return a value, if you need to return multiple values, you can only return an array or an object. Extension operators can provide workarounds
var datefields = readdatefields (database)varnew Date (... datefields)
The above code takes a row of data from the database, passing it directly to the constructor by extension operator date
2, Array.from ()
The Array.from () method is used to convert a class array object and a traversed object to a true array
var arraylike = { ' 0 ': ' es6 ' ' 1 ': ' node. js ' ' 2 ': ' Vue ' 3} // es5 var arr1 = [].slice.call ( Arraylike); Console.log (arr1) // ["Es6", " node. js "," Vue "] // es6 var arr2 = Array.from (arraylike) console.log (arr2) // ["Es6", "node. js", "Vue"]
In practice, a common array-like object is the NodeList collection returned by the DOM operation, as well as the arguments object inside the function. Array.from can convert them to real arrays.
<!DOCTYPE HTML><HTMLLang= "en"><Head> <MetaCharSet= "UTF-8"> <Metaname= "Viewport"content= "Width=device-width, initial-scale=1.0"> <Metahttp-equiv= "X-ua-compatible"content= "Ie=edge"> <title>Document</title></Head><Body> <P>Journey</P> <P>Three kingdoms</P> <P>Outlaws</P> <P>Dream</P><Script> varP=Document.queryselectorall ('P') varParray=Array.from (P) Parray.foreach (function(item) {Console.log (item.textcontent)})//Journey to the //Kingdoms //Outlaws //Red Mansions</Script></Body></HTML>
Array.from () can also accept the second parameter, which is used to process each element, putting the processed value into the returned array
Array.from ([1,3,5],function(item) {return item*2}) // [2, 6, ten]
3, Array.of ()
The Array.of () method is used to transfer a set of values to a group of
Array.of (3,4,5) // [3, 4, 5]array.of (3) // [3]Array.of (3). Length // 1
Array.of () always returns an array of parameter values. If there are no arguments, an empty array is returned
Array.of () // []
4. Copywithin () of the array instance
The Copywithin () method copies the members of the specified position to another location (overwriting the original member) inside the current array, and then returns the current array. In other words, using this method modifies the current array.
Array.prototype.copyWithin(target, start = 0, end = 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. (data that does not contain the end position)
These three parameters should all be numeric values, and if not, they will be automatically converted to numeric values
[1, 2, 3, 4, 5].copywithin (0, 3) // [4, 5, 3, 4,5]// The above example indicates / / 1, from subscript 3 The position (number 4) begins to read the data, until the last digit of the array // 2, the Read Data 4 and 5, starting with the position of the subscript 0 (number 1) to replace
//Copy the 3rd number to the No. 0 number bit, not including the data labeled 4[1, 2, 3, 4, 5].copywithin (0, 3, 4)//[4, 2, 3, 4, 5]//-2 is equivalent to 3rd digits, 1 is equivalent to the number of 4th digits[1, 2, 3, 4, 5].copywithin (0,-2,-1)//[4, 2, 3, 4, 5]//Copy the 3rd number to the number No. 0 bit[].copywithin.call ({length:5, 3:1}, 0, 3)//{0:1, 3:1, length:5}//end the number 2nd to the array and copy it to the number No. 0 bitvarI32A =NewInt32array ([1, 2, 3, 4, 5]); I32a.copywithin (0, 2);//Int32array [3, 4, 5, 4, 5]
5. Find () and FindIndex () for array instances
The Find method, which is used to find the first array member that meets the criteria. Its argument is a callback function, in which all array members execute the callback function sequentially until they find the first member to return a value of true, and then return the member. If there are no eligible members, the undefined is returned
[1,2,3,4,5,6].find (function(item) {return item>3}) // 4
The callback function of the Find method can accept three parameters, in turn, the current value, the current position, and the original array
[1, 5, 15].find (function(value, index, arr) { return value > 9;}) // ten
The FindIndex method, which is very similar to the Find method, returns the position of the first qualifying array member and returns 1 if all members do not meet the criteria.
[1,2,3,4,5,6].findindex (function(item) {return item>3}) // 4
In addition, both methods can find Nan, which makes up for the insufficiency of the IndexOf method of the array.
[Nan].indexof (Nan) // -1= object.is (nan, y)) // 0
In the above code, the IndexOf method does not recognize the Nan member of the array, but the FindIndex method can do so with the Object.is method
6. Includes () of the array instance
The Array.prototype.includes method returns a Boolean value that indicates whether a number group contains the given value, similar to the includes method of the string. ES2016 introduced this method.
[1, 2, 3].includes (2) // true[1, 2, 3].includes (4) // false[1, 2, NaN]. Includes (NaN) // 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 is the position of the reciprocal, and if it is larger than the array length (for example, the second argument is-4, but the array length is 3), it is reset to start at 0
[1, 2, 3].includes (3, 3); // false [1, 2, 3].includes (3,-1); // true
7. Entries () of the array instance, keys () and values ()
Keys () is the traversal of the key name
VALUES () is the traversal of a key value
Entries () is a traversal of key-value pairs
for(Let index of [' 1 ', ' C ', ' KS ')].keys ()) {Console.log (' Keys ', index);}//Keys 0//Keys 1//Keys 2 for(Let value of [' 1 ', ' C ', ' KS '].values ()) {Console.log (' Values ', value);}//values ' 1 '//values ' C '//values ' ks ' for(Let [index,value] of [' 1 ', ' C ', ' KS '].entries ()) {Console.log (' Entries ', Index,value);}//Entries 0 1//Entries 1 c//Entries 2 KS
Expansion of arrays