日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
詳解ES6數(shù)組

ES6是 JavaScript 的下一個版本標(biāo)準(zhǔn),2015.06 發(fā)版,ES6 主要是為了解決 ES5 的先天不足,比如 JavaScript 里并沒有類的概念,但是目前瀏覽器的 JavaScript 是 ES5 版本,大多數(shù)高版本的瀏覽器也支持 ES6,不過只實現(xiàn)了 ES6 的部分特性和功能,本篇文章重點為大家講解一下ES6數(shù)組。

創(chuàng)新互聯(lián)公司自2013年創(chuàng)立以來,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項目網(wǎng)站建設(shè)、成都網(wǎng)站設(shè)計網(wǎng)站策劃,項目實施與項目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元思茅做網(wǎng)站,已為上家服務(wù),為思茅各地企業(yè)和個人服務(wù),聯(lián)系電話:13518219792

數(shù)組創(chuàng)建

Array.of()

將參數(shù)中所有值作為元素形成數(shù)組。

console.log(Array.of(1, 2, 3, 4)); // [1, 2, 3, 4]

// 參數(shù)值可為不同類型
console.log(Array.of(1, '2', true)); // [1, '2', true]

// 參數(shù)為空時返回空數(shù)組
console.log(Array.of()); // []

Array.from()

將類數(shù)組對象或可迭代對象轉(zhuǎn)化為數(shù)組。

// 參數(shù)為數(shù)組,返回與原數(shù)組一樣的數(shù)組
console.log(Array.from([1, 2])); // [1, 2]

// 參數(shù)含空位
console.log(Array.from([1, , 3])); // [1, undefined, 3]

參數(shù)

Array.from(arrayLike[, mapFn[, thisArg]])

返回值為轉(zhuǎn)換后的數(shù)組。

arrayLike

想要轉(zhuǎn)換的類數(shù)組對象或可迭代對象。

console.log(Array.from([1, 2, 3])); // [1, 2, 3]

mapFn

可選,map 函數(shù),用于對每個元素進(jìn)行處理,放入數(shù)組的是處理后的元素。

console.log(Array.from([1, 2, 3], (n) => n * 2)); // [2, 4, 6]

thisArg

可選,用于指定 map 函數(shù)執(zhí)行時的 this 對象。

let map = {
   do: function(n) {
       return n * 2;
   }
}
let arrayLike = [1, 2, 3];
console.log(Array.from(arrayLike, function (n){
   return this.do(n);
}, map)); // [2, 4, 6]

類數(shù)組對象

一個類數(shù)組對象必須含有 length 屬性,且元素屬性名必須是數(shù)值或者可轉(zhuǎn)換為數(shù)值的字符。

let arr = Array.from({
 0: '1',
 1: '2',
 2: 3,
 length: 3
});
console.log(); // ['1', '2', 3]

// 沒有 length 屬性,則返回空數(shù)組
let array = Array.from({
 0: '1',
 1: '2',
 2: 3,
});
console.log(array); // []

// 元素屬性名不為數(shù)值且無法轉(zhuǎn)換為數(shù)值,返回長度為 length 元素值為 undefined 的數(shù)組  
let array1 = Array.from({
 a: 1,
 b: 2,
 length: 2
});
console.log(array1); // [undefined, undefined]

轉(zhuǎn)換可迭代對象

轉(zhuǎn)換 map

let map = new Map();
map.set('key0', 'value0');
map.set('key1', 'value1');
console.log(Array.from(map)); // [['key0', 'value0'],['key1',
// 'value1']]

轉(zhuǎn)換 set

let arr = [1, 2, 3];let set = new Set(arr);console.log(Array.from(set)); // [1, 2, 3]

轉(zhuǎn)換字符串

let str = 'abc';console.log(Array.from(str)); // ["a", "b", "c"]

擴(kuò)展的方法

查找

find()

查找數(shù)組中符合條件的元素,若有多個符合條件的元素,則返回第一個元素。

let arr = Array.of(1, 2, 3, 4);console.log(arr.find(item => item > 2)); // 3// 數(shù)組空位處理為 undefinedconsole.log([, 1].find(n => true)); // undefined

findIndex()

查找數(shù)組中符合條件的元素索引,若有多個符合條件的元素,則返回第一個元素索引。

let arr = Array.of(1, 2, 1, 3);// 參數(shù)1:回調(diào)函數(shù)// 參數(shù)2(可選):指定回調(diào)函數(shù)中的 this 值console.log(arr.findIndex(item => item = 1)); // 0// 數(shù)組空位處理為 undefinedconsole.log([, 1].findIndex(n => true)); //0

填充

fill()

將一定范圍索引的數(shù)組元素內(nèi)容填充為單個指定的值。

let arr = Array.of(1, 2, 3, 4);// 參數(shù)1:用來填充的值// 參數(shù)2:被填充的起始索引// 參數(shù)3(可選):被填充的結(jié)束索引,默認(rèn)為數(shù)組末尾console.log(arr.fill(0,1,2)); // [1, 0, 3, 4]

copyWithin()

將一定范圍索引的數(shù)組元素修改為此數(shù)組另一指定范圍索引的元素。

// 參數(shù)1:被修改的起始索引// 參數(shù)2:被用來覆蓋的數(shù)據(jù)的起始索引// 參數(shù)3(可選):被用來覆蓋的數(shù)據(jù)的結(jié)束索引,默認(rèn)為數(shù)組末尾console.log([1, 2, 3, 4].copyWithin(0,2,4)); // [3, 4, 3, 4]// 參數(shù)1為負(fù)數(shù)表示倒數(shù)console.log([1, 2, 3, 4].copyWithin(-2, 0)); // [1, 2, 1, 2]console.log([1, 2, ,4].copyWithin(0, 2, 4)); // [, 4, , 4]

遍歷

entries()

遍歷鍵值對。

for(let [key, value] of ['a', 'b'].entries()){   console.log(key, value);}// 0 "a"http:// 1 "b"http:// 不使用 for... of 循環(huán)let entries = ['a', 'b'].entries();console.log(entries.next().value); // [0, "a"]console.log(entries.next().value); // [1, "b"]// 數(shù)組含空位console.log([...[,'a'].entries()]); // [[0, undefined], [1, "a"]]

keys()

遍歷鍵名。

for(let key of ['a', 'b'].keys()){   console.log(key);}// 0// 1// 數(shù)組含空位console.log([...[,'a'].keys()]); // [0, 1]

values()

遍歷鍵值。

for(let value of ['a', 'b'].values()){   console.log(value);}// "a"http:// "b"http:// 數(shù)組含空位console.log([...[,'a'].values()]); // [undefined, "a"]

包含

includes()

數(shù)組是否包含指定值。

注意:與 Set 和 Map 的 has 方法區(qū)分;Set 的 has 方法用于查找值;Map 的 has 方法用于查找鍵名。

// 參數(shù)1:包含的指定值[1, 2, 3].includes(1);    // true// 參數(shù)2:可選,搜索的起始索引,默認(rèn)為0[1, 2, 3].includes(1, 2); // false// NaN 的包含判斷[1, NaN, 3].includes(NaN); // true

嵌套數(shù)組轉(zhuǎn)一維數(shù)組

flat()

console.log([1 ,[2, 3]].flat()); // [1, 2, 3]// 指定轉(zhuǎn)換的嵌套層數(shù)console.log([1, [2, [3, [4, 5]]]].flat(2)); // [1, 2, 3, [4, 5]]// 不管嵌套多少層console.log([1, [2, [3, [4, 5]]]].flat(Infinity)); // [1, 2, 3, 4, 5]// 自動跳過空位console.log([1, [2, , 3]].flat()); // [1, 2, 3]

flatMap() 先對數(shù)組中每個元素進(jìn)行了的處理,再對數(shù)組執(zhí)行 flat() 方法。

// 參數(shù)1:遍歷函數(shù),該遍歷函數(shù)可接受3個參數(shù):當(dāng)前元素、當(dāng)前元素索引、原數(shù)組// 參數(shù)2:指定遍歷函數(shù)中 this 的指向console.log([1, 2, 3].flatMap(n => [n * 2])); // [2, 4, 6]

數(shù)組緩沖區(qū)

數(shù)組緩沖區(qū)是內(nèi)存中的一段地址。

定型數(shù)組的基礎(chǔ)。

實際字節(jié)數(shù)在創(chuàng)建時確定,之后只可修改其中的數(shù)據(jù),不可修改大小。

創(chuàng)建數(shù)組緩沖區(qū)

通過構(gòu)造函數(shù)創(chuàng)建:

let buffer = new ArrayBuffer(10);
console.log(buffer.byteLength); // 10
分割已有數(shù)組緩沖區(qū)
let buffer = new ArrayBuffer(10);
let buffer1 = buffer.slice(1, 3);
console.log(buffer1.byteLength); // 2

視圖

視圖是用來操作內(nèi)存的接口。

視圖可以操作數(shù)組緩沖區(qū)或緩沖區(qū)字節(jié)的子集,并按照其中一種數(shù)值數(shù)據(jù)類型來讀取和寫入數(shù)據(jù)。

DataView 類型是一種通用的數(shù)組緩沖區(qū)視圖,其支持所有8種數(shù)值型數(shù)據(jù)類型。

創(chuàng)建:

// 默認(rèn) DataView 可操作數(shù)組緩沖區(qū)全部內(nèi)容
let buffer = new ArrayBuffer(10);
   dataView = new DataView(buffer);
dataView.setInt8(0,1);
console.log(dataView.getInt8(0)); // 1

// 通過設(shè)定偏移量(參數(shù)2)與長度(參數(shù)3)指定 DataView 可操作的字節(jié)范圍
let buffer1 = new ArrayBuffer(10);
   dataView1 = new DataView(buffer1, 0, 3);
dataView1.setInt8(5,1); // RangeError

定型數(shù)組

數(shù)組緩沖區(qū)的特定類型的視圖。

可以強制使用特定的數(shù)據(jù)類型,而不是使用通用的 DataView 對象來操作數(shù)組緩沖區(qū)。

創(chuàng)建

通過數(shù)組緩沖區(qū)生成

let buffer = new ArrayBuffer(10),
   view = new Int8Array(buffer);
console.log(view.byteLength); // 10

通過構(gòu)造函數(shù)

let view = new Int32Array(10);
console.log(view.byteLength); // 40
console.log(view.length);     // 10

// 不傳參則默認(rèn)長度為0
// 在這種情況下數(shù)組緩沖區(qū)分配不到空間,創(chuàng)建的定型數(shù)組不能用來保存數(shù)據(jù)
let view1 = new Int32Array();
console.log(view1.byteLength); // 0
console.log(view1.length);     // 0

// 可接受參數(shù)包括定型數(shù)組、可迭代對象、數(shù)組、類數(shù)組對象
let arr = Array.from({
 0: '1',
 1: '2',
 2: 3,
 length: 3
});
let view2 = new Int16Array([1, 2]),
   view3 = new Int32Array(view2),
   view4 = new Int16Array(new Set([1, 2, 3])),
   view5 = new Int16Array([1, 2, 3]),
   view6 = new Int16Array(arr);
console.log(view2 .buffer === view3.buffer); // false
console.log(view4.byteLength); // 6
console.log(view5.byteLength); // 6
console.log(view6.byteLength); // 6

注意要點

length 屬性不可寫,如果嘗試修改這個值,在非嚴(yán)格模式下會直接忽略該操作,在嚴(yán)格模式下會拋出錯誤。

let view = new Int16Array([1, 2]);
view.length = 3;
console.log(view.length); // 2

定型數(shù)組可使用 entries()、keys()、values()進(jìn)行迭代。

let view = new Int16Array([1, 2]);
for(let [k, v] of view.entries()){
   console.log(k, v);
}
// 0 1
// 1 2

find() 等方法也可用于定型數(shù)組,但是定型數(shù)組中的方法會額外檢查數(shù)值類型是否安全,也會通過 Symbol.species 確認(rèn)方法的返回值是定型數(shù)組而非普通數(shù)組。concat() 方法由于兩個定型數(shù)組合并結(jié)果不確定,故不能用于定型數(shù)組;另外,由于定型數(shù)組的尺寸不可更改,可以改變數(shù)組的尺寸的方法,例如 splice() ,不適用于定型數(shù)組。

let view = new Int16Array([1, 2]);
view.find((n) > 1); // 2

所有定型數(shù)組都含有靜態(tài) of() 方法和 from() 方法,運行效果分別與 Array.of() 方法和 Array.from() 方法相似,區(qū)別是定型數(shù)組的方法返回定型數(shù)組,而普通數(shù)組的方法返回普通數(shù)組。

let view = Int16Array.of(1, 2);
console.log(view instanceof Int16Array); // true

定型數(shù)組不是普通數(shù)組,不繼承自 Array 。

let view = new Int16Array([1, 2]);
console.log(Array.isArray(view)); // false

定型數(shù)組中增加了 set() 與 subarray() 方法。 set() 方法用于將其他數(shù)組復(fù)制到已有定型數(shù)組, subarray() 用于提取已有定型數(shù)組的一部分形成新的定型數(shù)組。

// set 方法
// 參數(shù)1:一個定型數(shù)組或普通數(shù)組
// 參數(shù)2:可選,偏移量,開始插入數(shù)據(jù)的位置,默認(rèn)為0
let view= new Int16Array(4);
view.set([1, 2]);
view.set([3, 4], 2);
console.log(view); // [1, 2, 3, 4]

// subarray 方法
// 參數(shù)1:可選,開始位置
// 參數(shù)2:可選,結(jié)束位置(不包含結(jié)束位置)
let view= new Int16Array([1, 2, 3, 4]),
   subview1 = view.subarray(),
   subview2 = view.subarray(1),
   subview3 = view.subarray(1, 3);
console.log(subview1); // [1, 2, 3, 4]
console.log(subview2); // [2, 3, 4]
console.log(subview3); // [2, 3]

擴(kuò)展運算符

復(fù)制數(shù)組

let arr = [1, 2],
   arr1 = [...arr];
console.log(arr1); // [1, 2]

// 數(shù)組含空位
let arr2 = [1, , 3],
   arr3 = [...arr2];
console.log(arr3); [1, undefined, 3]

合并數(shù)組

console.log([...[1, 2],...[3, 4]]); // [1, 2, 3, 4]

新聞名稱:詳解ES6數(shù)組
地址分享:http://www.5511xx.com/article/cdsojss.html