ES6 array new API

ES6 array new API

name type Introduction
of() static method Used to convert a set of values ​​into an array. Note the difference with Array
from() static method Convert two types of objects into real arrays (arguments, element collection)
find() instance method Used to find the first array member that meets the criteria
findIndex() instance method Returns the position of the first array member that meets the criteria, or if none of the members meet the criteria-1
fill() instance method Fills an array with the given values. Optional start index and end index
copyWithin(targetIndex,[start],[end]) instance method Inside the current array, copy the member at the specified position to another position, and then return the current array
includes(data,[index]) instance method Returns a boolean indicating whether an array contains the given value, optionally starting index

1.Array.of()

Used to convert a set of values ​​into an array

  • Example:
let arr1 = Array.of(1, 2, 3, 4, 5)
console.log(arr1) //[1,2,3,4,5]
let arr2 = Array.of(4)
console.log(arr2)  //[4]
  • The difference between Array and Array of():

    It is reflected in passing a parameter

    When Array passes a parameter, the elements in the array are empty;

    When Array.of() passes a parameter, the elements in the array are the values ​​of the passed parameter;

    • Array
    let arr0 = new Array(4); 
    console.log(arr0);// [empty,empty,empty,empty]
    
    • Array.of()
    let arr2 = Array.of(4)
    console.log(arr2); //[4]
    

2.Array.form()

Convert two types of objects (arguments, element collection, custom class array ['0':100, length:1]) into real arrays

  • Example:
//获取p标签
let pEles = document.getElementsByTagName('p');
console.log(pEles);
//遍历
Array.from(pEles).forEach(item => console.log(item));
//由此可以说明把元素的集合转为真正的数组

3.find()

Used to find the first eligible array member and return the array element

If there is no array element that satisfies the condition, the method returns undefined

  • Example:
arr1=[1, 2, 3, 4, 5]
let num1 = arr1.find(function(item) {
    
    
    return item > 1;
});
console.log(num1); //2

4.findIndex()

Used to find the index value of the first qualified array member and return the index value of the array element

If there is no array element that satisfies the condition, the method returns -1

  • Example:
arr1=[1, 7, 9, 4, 5]
let num1 = arr1.findIndex(item => item > 9);
console.log(num1); //-1
let num2 = arr1.findIndex(item => item > 3);
console.log(num2); //2

5.fill()

The index specified for the array is filled with the specified value

//—the first parameter is the filled value

//—The second parameter is the starting index, the default value is 0

//—the third parameter is the ending index (exclusive) default value is array length

  • Example:
arr1=[1, 2, 3, 4, 5]
console.log(arr1); //[1, 2, 3, 4, 5]
arr1.fill(6);
console.log(arr1); //[6, 6, 6, 6, 6]
arr1.fill(7, 2);
console.log(arr1); //[6, 6, 7, 7, 7]
arr1.fill(8, 1, 3);
console.log(arr1); //[6, 8, 8, 7, 7]

6.copyWithin()

Inside the current array, copy the member at the specified position to another position, and then return the current array

// — the second parameter is the starting index

// —The third parameter is the end index **(not included)**, assign the corresponding array element to the beginning index set by the first parameter, and replace the value on the original index in turn

  • Example:
arr1=[1, 2, 3, 4, 5]
let arr4 = arr1.copyWithin(0, 1, 4);
console.log(arr4); //[2, 3, 4, 4, 5]
let arr5 = arr4.copyWithin(1, 3, 4);
console.log(arr5); //[2, 4, 4, 4, 5]
let arr6 = arr5.copyWithin(0, 2, 4);
console.log(arr6); //[4, 4, 4, 4, 5]

7.includes()

Determine whether the array contains the specified value—is a congruent judgment

Equivalent to Object.is()

//—the first value is the specified value contained

//—the second value is the index of the specified value

  • Example:
arr5=[2, 3, 4, 7, 5]
let flag = arr5.includes(2);
console.log(flag);//true
let flag1 = arr5.includes(3,1);
console.log(flag1);//true

Guess you like

Origin blog.csdn.net/SH744/article/details/126858898