Array.of () function
Function: Converts a set of values into an array.
1 Array.of (1,2,3,4,5); 2 Results: [1,2,3,4,5]
is not very well understood, the number passed in is: The pass, finally through the Array.of function processing, got an array, and the contents of the array is [1,2,3,4,5].
Array.from () function
Function: You can convert an array-like object or an object that can be traversed into a real array.
What is an array-like object? The most common is to call the getElementsByTagName method to get the result, it is a similar array of results, getElementsByTagName method must be unfamiliar, we look at:
1 Let ele = document.getElementsByTagName (' a '); 2 ele instanceof Array; Result: false, not array 3 ele instanceof object;//Result: true
We call the getElementsByTagName method, get the result stored in the variable ele, and then type judgment, found that the variable ele is not an array, but an object array, an array-like object, and then we use the Array.from () to process it, and then make the type judgement again.
1 Array.from (ele) instanceof Array; 2 Result: True, is an array
This time we run the result: true, that is, after the ARRAY.FROM function processing returned results, has become a real array.
One of the uses of the Array.from function is to convert a string to an array. Look at the following case:
1 Let str = ' Hello '; 2 array.from (str); 3 Results: ["H", "E", "L", "L", "O"]
The returned result of the string "Hello" has been transformed into an array: ["H", "E", "L", "L", "O"].
In addition to the two static methods described above, a number of new instance methods are added.
Find () function
Function: Finds the first element in the array that matches the condition.
1 Let arr = [1,2,3,4,5,6]; 2 arr.find (function (value) {3 return value > 2; 4 }); 5 Results: value=3
Looking at the code above, the Find () function's argument is an anonymous function, and each element of the array goes into the anonymous function execution until the result is the True,find function returns the value: 3. If all elements do not conform to the conditions of the anonymous function, the Find () function returns Undefind. Look at the following code case:
1 Let arr = [1,2,3,4,5,6]; 2 arr.find (function (value) {3 return value > 7; 4 }); 5 Results: undefined
In the above case, no element greater than 7 exists in the array, so the Find function returns: undefined.
FindIndex () function
Function: Returns the position of the first array member that matches a condition.
1 Let arr = [7,8,9,10]; 2 Arr.findindex (function (value) {3 return value > 8; 4 }); 5 Results: 2
The result of the above code is: 2, because the element in the array element is greater than 8, and the element 9 is in the formal 2, (the array element is counted from 0). If all elements do not conform to the conditions of the anonymous function, the FindIndex () function returns-1.
Fill () function
Function: Fills the array with the specified value.
1 Let arr = [n/a]; 2 Arr.fill (4); 3 Results: [4,4,4]
The array after the fill () function has been processed, arr has become [4,4,4]; just as the function name fills (fill). All elements are populated with the number 4. If I wanted to just fill in some elements, could it? Yes, the Fill () function provides parameters that specify the starting and ending positions of the fill.
Or the above case, let's make a slight adjustment and then demonstrate:
1 Let arr = [n/a]; 2 Arr.fill (4,1,3); 3 Results: [1,4,4]
The 2nd and 3rd parameters in the above code mean that the element starting at position 1 is filled with the number 4, until position 3, so that the element of position 1 and position 2 is filled with the number 4, resulting in the result: [1,4,4].
Entries () function
Function: An array of key-value pairs are traversed, returning a walker that can be used for: of to traverse it.
Walker and for: The is also a new feature of ES6, we have not yet introduced, we can start with a simple understanding.
Back to our entries () function, take a look at the case:.
1 for (let [i,v] of [' A ', ' B '].entries ()) 2 {3 console.log (i,v); 4 }5 //0 "a"6 //1 "B"
In the above code, we will return a entries () function of a walker, with For...of to traverse, and print out the results, you can get the array of key values: 0 and 1, as well as the corresponding array elements: ' A ' and ' B '.
If you only want to traverse the index key of an array, you can use another instance method.
keys () function
Function: The index key of the array is traversed, and a walker is returned.
1 for (Let index of [' A ', ' B '].keys ())2 {3 console.log (index); 4 }5 //06 //17
As shown above, we have printed out the index keys for the array: 0 and 1.
If we want to traverse only the elements of an array, we can use 21 instance methods.
VALUES () function
Function: Iterates through the elements of an array, returning a walker.
1 for (let value of [' A ', ' B '].values ())2 {3 console.log (value); 4 }5 //a6 //b
The result of the above code is also what we expected, and we finally printed out the elements of the array: A and B.
In addition to new functions, ES6 brings a new concept to arrays: array derivation.
Array derivation
Array derivation: Create a new array directly from an existing array, using a concise notation.
For example: By adding we have an array, I multiply each element by 2 on the basis of this array to get a new array.
Let's look at the traditional implementation method:
1 var arr1 = [1,2,3,4]; 2 var arr2 = []; 3 For (Let I=0;i<arr1length;i++) {4 //per element multiplied by 2, push to array arr2 5 Arr2.push (arr1[i]*2); 6 }7 console.log (arr2); 8 //Result: [2,4,6,8]
Traditional way, in addition to the above way, we can also be implemented using the Foreach method. But what we're going to introduce is the new notation provided by ES6: array derivation.
1 var arr1 = [1, 2, 3, 4]; 2 var arr2 = [for (I of arr1) I * 2]; 3 Console.log (arr2); 4 Results: [2, 4, 6, 8]
We can also get the array arr2, which has the same value as the previous code. And the amount of code is greatly reduced. This is the convenience that the array derivation brings to us.
In the array derivation, for: You can also add an if statement, which we look at in the code case:
1 var array1 = [1, 2, 3, 4]; 2 var array2 = [for (I of Array1) if (i>3) i]; 3 Console.log (array2); 4 Results: [4]
In the above code, we are no longer multiplying the elements of an array by 2, but using the if to make a judgment: the elements of the array are greater than 3. So the result of the operation is: [4].
Well, the expansion of the array is about the same, mainly the introduction of the use of some new functions, these functions are very convenient, for developers to save a lot of code. If there is doubt, you can leave a message to ask questions, front-end June will reply.
Summary of this section
Summary: ES6 brings many useful methods to the array: Array.of (), Array.from (), find (), FindIndex (), fill (), entries (), values (), plus a more concise syntax: array derivation, Makes it easier for us to generate a new array.
ES6 Array Extension