日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

ES6數(shù)組新增API

作者:H-hang 更新時(shí)間: 2022-09-22 編程語言

ES6數(shù)組新增API

名稱 類型 簡介
of() 靜態(tài)方法 用于將一組值,轉(zhuǎn)換為數(shù)組。注意跟Array的區(qū)別
from() 靜態(tài)方法 將兩類對象轉(zhuǎn)為真正的數(shù)組(arguments,元素集合)
find() 實(shí)例方法 用于找出第一個(gè)符合條件的數(shù)組成員
findIndex() 實(shí)例方法 返回第一個(gè)符合條件的數(shù)組成員的位置,如果所有成員都不符合條件,則返回-1
fill() 實(shí)例方法 使用給定值,填充一個(gè)數(shù)組。可選開始索引和結(jié)束索引
copyWithin(targetIndex,[start],[end]) 實(shí)例方法 在當(dāng)前數(shù)組內(nèi)部,將指定位置的成員復(fù)制到其他位置,然后返回當(dāng)前數(shù)組
includes(data,[index]) 實(shí)例方法 返回一個(gè)布爾值,表示某個(gè)數(shù)組是否包含給定的值,可選開始索引

1.Array.of()

用于將一組值,轉(zhuǎn)換為數(shù)組

  • 示例:
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]
  • Array和Array of()的區(qū)別:

    體現(xiàn)在傳一個(gè)參數(shù)的時(shí)候

    當(dāng)Array傳一個(gè)參數(shù)時(shí),數(shù)組中的元素為empty;

    當(dāng)Array.of()傳一個(gè)參數(shù)時(shí),數(shù)組中的元素是傳入的參數(shù)的值;

    • 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()

將兩類對象(arguments,元素集合,自定義類數(shù)組[‘0’:100,length:1])轉(zhuǎn)為真正的數(shù)組

  • 示例:
//獲取p標(biāo)簽
let pEles = document.getElementsByTagName('p');
console.log(pEles);
//遍歷
Array.from(pEles).forEach(item => console.log(item));
//由此可以說明把元素的集合轉(zhuǎn)為真正的數(shù)組

3.find()

用于找出第一個(gè)符合條件的數(shù)組成員,并且返回該數(shù)組元素

如果沒有滿足條件的數(shù)組元素該方法返回undefined

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

4.findIndex()

用于找出第一個(gè)符合條件的數(shù)組成員的索引值,并且返回該數(shù)組元素的索引值

如果沒有滿足條件的數(shù)組元素該方法返回-1

  • 示例:
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()

給數(shù)組指定的索引,以指定值進(jìn)行填充

//—第一個(gè)參數(shù)是填充的值

//—第二個(gè)參數(shù)是起始索引 默認(rèn)值是0

//—第三個(gè)參數(shù)是結(jié)束的索引(不包含) 默認(rèn)值是數(shù)組length

  • 示例:
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()

在當(dāng)前數(shù)組內(nèi)部,將指定位置的成員復(fù)制到其他位置,然后返回當(dāng)前數(shù)組

// —第二個(gè)參數(shù)為起始索引

// —第三個(gè)參數(shù)為結(jié)束索引**(不包含)**,將對應(yīng)的數(shù)組元素賦值給第一個(gè)參數(shù)設(shè)置的開頭的索引,依次取替換原索引上的值

  • 示例:
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()

判斷數(shù)組是否包含指定值—是全等判斷

相當(dāng)于Object.is()

//—第一個(gè)值是包含的指定值

//—第二個(gè)值是指定值的索引

  • 示例:
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

原文鏈接:https://blog.csdn.net/SH744/article/details/126858898

欄目分類
最近更新