ES6 extensions to arrays

1. Array.of() function

Function role: convert a set of values ​​into an array
var arr=Array.of(1,2,3,4)
		console.log(arr) // [1,2,3,4]

2. Array.from() function

1. You can convert array-like objects or traversable objects into real arrays.
For example: getElementsByTagName, etc.
let ele = document.getElementsByTagName('a');    ele instanceof Array;  //结果:false,不是数组    ele instanceof Object; //结果:true,是对象
  Array.from(ele) instanceof Array;    //结果:true,是数组
2. Convert the string to an array
 let str='hello';
 let newarr=Array.from(str)
   console.log(newarr) // ['h','e','l','l','o']

3. find() function

Function: Find the first element in the array that meets the conditions.
let arr=[1,3,5,6,7]
    let x=arr.find(function(num){
    	return num<8 // 1
    	num>2 //3
    	num>9 // underfined
    })
It can be seen that the parameter of the find() function is an anonymous function. Each element of the array will enter the anonymous function for execution until the result is true, and the find function will return the value of the value. If all elements do not meet the conditions of the anonymous function, The find( ) function will return undefined.

4. findIndex() function

Function: Returns the position of the first array member that meets the condition.
let arr=[1,3,5,6,7]
    let x=arr.findIndex(function(num){
    	return num<8 // 0
    	 num>2 // 1
    	 num>9   // -1
    })
It can be seen that the array elements are counted from 0; if the conditions of the anonymous function are not met, the findIndex( ) function will return -1.

Five, fill (filled elements, starting position, ending position) function

Function: Fill the array with the specified value.
let arr=[1,3,5,6,7]
    arr.fill(10,0,1)
    console.log(arr) // [10,3,5,6,7]
It can be seen that the value of the original array will be overwritten

6. The entries( ) function

Function: Traverse the key-value pairs of the array and return a traverser, which can be traversed with for..of.
for(let[key,value] of ['aa','bb'].entries()){
	console.log(key,value); // 0 'aa' 1 'bb'
}

7. keys( ) function

Iterates over the index keys of the array, returning a iterator.
for(let index of ['aa','bb'].keys()){
	console.log(index); // 0 1 
}

Eight, values ​​( ) function

Function: Traverse the elements of the array and return a traverser.
 for(let value of ['aa','bb'].values()){
    	console.log(value); // aa bb 
 }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324458667&siteId=291194637