ES6 --- new array array method (reprint)

Original Address: https://blog.csdn.net/Wbiokr/article/details/65939582

 

1. find

find parameters for callback, the callback function may receive three parameters, values ​​of x, so i, the array ARR, a default callback function returns the value x.

ARR = the let [1,2,234, 'SDF', - 2 ]; 
arr.find ( function (x) {
     return x <= 2 ; 
}) // Result: 1, returns the first matching x-value 
arr. Find ( function (X, I, ARR) {
     IF (X <2 ) {the console.log (X, I, ARR)} 
}) // results: 1 0 [1, 2, 234, "sdf", -2 ], - 2 4 [1, 2, 234, "sdf", -2]

 

2.findIndex

findIndex and find the same, but the default is the index return.

ARR = the let [1,2,234, 'SDF', - 2 ]; 
arr.findIndex ( function (x) {
     return x <= 2 ; 
}) // Result: 0, returns the first matching x-value index 
arr.findIndex ( function (X, I, ARR) {
     IF (X <2 ) {the console.log (X, I, ARR)} 
}) // results: 1 0 [1, 2, 234, "sdf", -2], - 2 4 [1 , 2, 234, "sdf", -2]

 

3.includes

Includes function and includes string, like receiving the second parameter, the query and the query term starting position.

ARR = the let [1,2,234, 'SDF', - 2 ]; 
arr.includes ( 2); // results true, returns a boolean 
arr.includes (20 is); // Results: false, returns a Boolean value 
arr.includes (2,3) // result: false, returns a Boolean value

 

4.keys

keys, traversing the array index

ARR = the let [1,2,234, 'SDF', - 2 ];
 for (arr.keys of the let A ()) { 
    the console.log (A) 
} // Results: 0,1,2,3,4 traversed the index of the array arr

 

5.values

values, traversal of the array item

ARR = the let [1,2,234, 'SDF', - 2 ];
 for (arr.values of the let A ()) { 
    the console.log (A) 
} // Results: 1,2,234, sdf, -2 traversed array the value of arr

 

6.entries

entries, traverse the array of key-value pairs.

let arr=['w','b'];
for(let a of arr.entries()){
    console.log(a)
}//结果:[0,w],[1,b]
for(let [i,v] of arr.entries()){
    console.log(i,v)
}//结果:0 w,1 b

 

7.fill

fill method of changing the original array, when the third parameter is greater than the length of the array, when in the end position of the last bit.

ARR = the let [ 'W', 'B' ]; 
arr.fill ( 'I') // Results: [ 'i', 'i '], changing the original array 
arr.fill ( 'O',. 1) // results: [ 'i', 'o '] to change the original array, the second parameter indicates the starting position to fill 
new new the array (. 3) .fill ( 'K') fill ( 'R & lt', 1,2). // results : [ 'k', 'r ', 'k'], the third array represents the end position of the filling

 

8.Array.of

Array.of () method returns an array always, regardless of the type of argument, only a fractional amount, in an amount of 0 to return an empty array.

Array.of ( 'W', 'I', 'R & lt') // [ "W", "I", "R & lt"] Returns an array 
Array.of ([ 'W', 'O']) // [ [ 'w', 'o' ]] Back nested array 
Array.of (undefined) // [undefined] returns an array still 
Array.of () // [] returns an empty array

 

9.copyWithin

copyWithin method receives three parameters, the beginning of the data to be replaced, the replacement block at the beginning, at the end of the replacement block (not included); copyWithin (s, m, n).

[ "W", "I", "R & lt"]. copyWithin (0) // At this time constant array 
[ "W", "I", "R & lt"]. copyWithin (. 1) // [ "W", "w", "i"] , the start position of the array from the original array 1 is covered, before the entry of only 10 remain unchanged 
[ "w", "i" , "r", "b"]. copyWithin (1, 2) // [ "W", "R & lt", "B", "B"], the index of the last 2 r, b are two alternative to the original array 1 began when an insufficient number of, termination becomes 
[ "W", "I", "R & lt", 'B']. copyWithin (l, 2,3) // [ "W", "R & lt", "R & lt", "B"], a strong item 1 r i is replaced item 2

 

10.Array.from

Array.from can lenght object with properties similar to the array to an array, it can also be traversed the object character strings into an array that receives two parameters, and converting the object callback

Array.from ({ '0': 'W', '. 1': 'B', length: 2}) // [ "W", "B"], returns the length of the array depends on the object length, therefore item must have! 
Array.from ({ '0': 'W', '. 1': 'B', length:. 4}) // [ "W", "B", undefined, undefined], the array 2 is not assigned to attributes therefore undefined 
Array.from ({ '0': 'W', '. 1': 'B', length:. 1}) // [ "W"], of length less than the number key, an array of sequential addition 

// / /////////////////////////// 
the let divs = document.getElementsByTagName ( 'div' ); 
Array.from (divs) // returns div element array 
Array.from ( 'wbiokr') // [ "W", "B", "I", "O", "K", "R & lt"] 
the array.

 

Guess you like

Origin www.cnblogs.com/lgqing920/p/11464905.html