1.1 extension Operators 1.1.1: ...
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.
<body> <div></div> <div></div> <div></div> <div ></div> </body> <script type= "Text/javascript" > console.log (... [N-a]); // 1 2 3 Console.log (1,... [2,3,4],5]; // 1 2 3) 4 5 Console.log ([... document.queryselectorall (' div ')]); // [Div, Div, Div, div] Select all div in page </script>
This operator is primarily used for function calls.
function Add (x, y) { return x + y; } var numbers = [4,]; // the
In the above code, the add(...numbers) function is called, and it uses the extension operator. The operator converts an array into a sequence of arguments.
1.1.2: Replace array apply method
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.
function f (x, Y, z) { console.log (x); Console.log (y); Console.log (z); } = [0, 1, 2]; F (.... args); // 0 1 2
// the writing of ES6 Math.max (... [3, +]) // equivalent to Math.max (14, 3, 77);
In the above code, since JavaScript does not mention the function of the largest element of the supply and demand array, it can only be applied Math.max to a function, convert the array to a sequence of arguments, and then find the maximum value. With the extension operator, you can use it directly Math.max .
Merging arrays
var = [arr1]; var arr2 = [3]; var arr3 = [4,5]; Console.log ([... arr1,... arr2,... arr3]); // [1,2,3,4,5]
Combined with deconstructed assignment
const [First, ... rest] = [1, 2, 3, 4, 5// 1rest // [2, 3, 4, 5]= // undefinedrest // []
String
The extension operator can also convert a string to a true array.
[... ' Hello '] // ["H", "E", "L", "L", "O"]
1.2array.from ()
The Array.from Array-like method is used to convert two types of objects to a true array: An array-like object (A. iterable) and an object that can be traversed (including ES6 new data structure set and map).
var arraylike = { ' 0 ': ' A ', ' 1 ': ' B ', ' 2 ': ' C ', 3}; // [' A ', ' B ', ' C ']
1.3array.of ()
Array.ofmethod is used to convert a set of values into an array.
// [3,11,8] // [3] // 1
The main purpose of this method is to compensate Array() for the insufficiency of the array constructor function. Because of the different number of arguments, Array() there are differences in behavior.
// []// [,,,]// [3, one, 8]
In the above code, the Array method has no parameters, one parameter, and three parameters, and the result is different. A Array() new array consisting of parameters is returned only if the number of arguments is not less than 2. When there is only one parameter number, it is actually the length of the specified array.
Array.ofcan basically be used to replace Array() or new Array() , and there are no overloads caused by different parameters. Its behavior is very uniform.
// []// [undefined]// [1]// [1, 2]
Array.ofAlways returns an array of parameter values. If there are no arguments, an empty array is returned.
1.4Copyeithin ()
The method of an array instance, within the copyWithin current array, copies the members of the specified position to another location (overwriting the original member), 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.
[1, 2, 3, 4, 5].copywithin (0, 3)// [4, 5, 3, 4, 5]
The code above indicates that the members (4 and 5), from the 3rd to the end of the array, are copied to the position starting from the No. 0 bit, and the result overwrites the original 1 and 2.
1.5 array find () and FindIndex ()
A method of an array instance that is find used to find the first qualifying array member. Its argument is a callback function, in which all array members execute the callback function sequentially until they find the first member that returns a value true , and then return the member. Returns if there are no members that meet the criteria undefined .
[1, 4, -5, 10].find ((n) = n < 0)// -5
The above code finds the first member in the array that is less than 0.
findThe callback function of a method can accept three parameters, which are the current value, the current position, and the original array.
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 .
1.6 Array fill ()
fillmethod fills an array with the given value.
[' A ', ' B ', ' C '].fill (7)// [7, 7, 7]new Array (3). Fill (7)// [ 7, 7, 7]
fillThe method can also accept the second and third parameters, which specify the starting and ending positions of the fill.
[' A ', ' B ', ' C '].fill (7, 1, 2)// [' A ', 7, ' C ']
The above code indicates that the fill method starts with the 1th number, fills the original array with 7, and ends before the 2nd-bit position.
1.7 Array entries ()/keys ()/values ()
ES6 provides three new methods-- entries() , keys() and values() --for iterating through an array. They all return a Walker object that can be traversed with a loop, the only difference being the traversal of the key name, the traversal of the key for...of keys() values() value, and the entries() traversal of the key-value pair.
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"
If you do not use for...of loops, you can manually invoke the method of the Walker object to next traverse it.
var letter = [' A ', ' B ', ' C ']; var entries =// [0, ' a ']// [1, ' B ']// [2, ' C ']
1.8 Array indludes ()
Array.prototype.includesmethod returns a Boolean value that indicates whether or not a number group contains the given value, similar to the method of a string includes . ES2016 introduced this method.
[1, 2, 3].includes (2) // true[1, 2, 3].includes (4) // false// 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, and if it is larger than the array length (for example, the second argument is -4 , but the array length is), it is reset 3 to 0 start.
[1, 2, 3].includes (3, 3); // false // true
A new method of arrays in ES6