table of Contents
Expanding array
A Extended operator
Meaning: Extended operator is three dots (...), the role is to convert the array is a sequence of parameters separated by commas
console.log(1,...[2,3,4],5); //等价于 console.log(1,2,3,4,5);
usage:
- The operator is mainly used to call the function
- Normal extended operator function parameters may be used in combination
- Operators can be placed behind the extension expression
- If the operator is empty array extension, it has no effect
function func(a,b,c,d,e){} //调用 func(1,...[2,3],4,...[5]) func(1,...(x > 0 ? [2,3,4,5] : [6,7,8,9]))
Application of extended operator
- Copy the array
const a1 = [2,3,4]; //如下两种方式都可以实现数组的复制 const b1 = [...a1]; const [...b2] = a1;
- Array merge
const a1 = [1,2,3]; const a2 = [4,5,6]; const b1 = [...a1,...a2];
- Combined with the destructuring assignment, generating an array
let [a,...list] = [1,2,3,4,5,6];
- If you extend the array assignment operator is used only on the last one, otherwise an error
let [a,...nums,c] = [1,2,3,4,5]; //报错
- The string to an array
[...'hello'] //['h','e','l','l','o']
II. Examples of the method of the array
1. Array.from()
Array.from for the two types of objects into real array
- Similar arrays of objects
- You can traverse objects
Any object has a length property, can be converted to an array by Array.from
If the parameter is an array, Array.form returns a new array of identical
If the parameter is a string, Array.from method will string into character array
Array.from can also accept a second parameter, map array similar to the method used to perform the processing for each element, the values of the array returned into the process
Array.from(arrList,x => x * x); //等价于 Array.from(arrList).map(x => x * x) Array.from(...[1,2,3],x => x * x); // ----> [1,4,9]
2. Array.of()
For a set of values, into an array
If there are no parameters, an empty array is returned
Array.of(); // [] Array.of(1); // [1] Array.of(1,2,3); //[1,2,3]
3. find () and findIndex ()
The method used to find the array to identify an eligible element parameter is a callback function
[1,2,3,4,5].find(n => n > 1); // 2
findIndex method returns the first qualifying position of a member of an array parameter is a callback function
[1,2,3,4,5].findIndex(n => n > 0) //返回0,即第一一个符合条件元素的下标
4. fill()
With the specified value, filling the array
fill method may also have the second and third parameter is used to specify start and end positions
[1,2,3].fill(7); //[7,7,7] [1,2,3,4,5].fill(8,0,3) //[8,8,8,4,5]
5. entries(),keys(),values()
entries to traverse the key-value pairs
keys used to iterate over the keys
values used to iterate values
Available for ... of to loop through
for(let index of ['a','b'].keys()){ console.log(index); } for(let ele of ['a','b'].values()){ console.log(ele); } for(let [index,ele] of ['a','b'].entries()){ console.log(index,ele); }
6. includes the method
includes an array of methods for an array contains the given value and returns a Boolean value
The second parameter indicates the start of the search process of
[1,2,3].includes(3); //true [1,2,3].includes(1,1); //false
7. flat(),flatMap()
Array flat leveling function for nested into one-dimensional array
[1,2,[3,4,5]].flat(); // [1,2,3,4,5]
flat layer of embedded arrays default only leveled, flattened layers can develop mass participation by the way, the parameter defaults to 1
No matter how many layers of nested, can be passed as a parameter by Infinity keywords, unified into one-dimensional arrays
8. copyWithin()
The method copyWithin array instance, for the current internal array, copying the member of the specified location to another location, and then returns the current array
It accepts three parameters
- target: the array from that position to begin replacing
- start: Alternatively, from the position to start reading the array
- end: Alternatively, the position to stop reading the array
[1,2,3,4,5].copyWithin(0,3); //[4,5,3,4,5]
9. array vacancy
- Array array, i.e. gap only be separated by commas, and there is no set position value
- Since each of the different methods of processing space, so we should try to avoid vacancy
to sum up
- ES6, add extended operator array, extended operator is analogous to rest parameter array into effect is a comma-separated list of parameters
- ES6, add some methods arrays object instance