| ES6 主要是為了解決 ES5 的先天不足,比如 JavaScript 里并沒有類的概念,但是目前瀏覽器的 JavaScript 是 ES5 版本,大多數高版本的瀏覽器也支持 ES6,不過只實現了 ES6 的部分特性和功能。 |

我們提供的服務有:網站建設、做網站、微信公眾號開發(fā)、網站優(yōu)化、網站認證、阿克蘇ssl等。為數千家企事業(yè)單位解決了網站和推廣的問題。提供周到的售前咨詢和貼心的售后服務,是有科學管理、有技術的阿克蘇網站制作公司
將參數中所有值作為元素形成數組。
console.log(Array.of(1, 2, 3, 4)); // [1, 2, 3, 4] // 參數值可為不同類型 console.log(Array.of(1, '2', true)); // [1, '2', true] // 參數為空時返回空數組 console.log(Array.of()); // []
將類數組對象或可迭代對象轉化為數組。
// 參數為數組,返回與原數組一樣的數組 console.log(Array.from([1, 2])); // [1, 2] // 參數含空位 console.log(Array.from([1, , 3])); // [1, undefined, 3]
參數
Array.from(arrayLike[, mapFn[, thisArg]])
返回值為轉換后的數組。
arrayLike
想要轉換的類數組對象或可迭代對象。
console.log(Array.from([1, 2, 3])); // [1, 2, 3]
mapFn
可選,map 函數,用于對每個元素進行處理,放入數組的是處理后的元素。
console.log(Array.from([1, 2, 3], (n) => n * 2)); // [2, 4, 6]
thisArg
可選,用于指定 map 函數執(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]
一個類數組對象必須含有 length 屬性,且元素屬性名必須是數值或者可轉換為數值的字符。
let arr = Array.from({
0: '1',
1: '2',
2: 3,
length: 3
});
console.log(); // ['1', '2', 3]
// 沒有 length 屬性,則返回空數組
let array = Array.from({
0: '1',
1: '2',
2: 3,
});
console.log(array); // []
// 元素屬性名不為數值且無法轉換為數值,返回長度為 length 元素值為 undefined 的數組
let array1 = Array.from({
a: 1,
b: 2,
length: 2
});
console.log(array1); // [undefined, undefined]
轉換 map
let map = new Map();
map.set('key0', 'value0');
map.set('key1', 'value1');
console.log(Array.from(map)); // [['key0', 'value0'],['key1',
// 'value1']]轉換 set
let arr = [1, 2, 3]; let set = new Set(arr); console.log(Array.from(set)); // [1, 2, 3]
轉換字符串
let str = 'abc'; console.log(Array.from(str)); // ["a", "b", "c"]
find()
查找數組中符合條件的元素,若有多個符合條件的元素,則返回第一個元素。
let arr = Array.of(1, 2, 3, 4); console.log(arr.find(item => item > 2)); // 3 // 數組空位處理為 undefined console.log([, 1].find(n => true)); // undefined
findIndex()
查找數組中符合條件的元素索引,若有多個符合條件的元素,則返回第一個元素索引。
let arr = Array.of(1, 2, 1, 3); // 參數1:回調函數 // 參數2(可選):指定回調函數中的 this 值 console.log(arr.findIndex(item => item = 1)); // 0 // 數組空位處理為 undefined console.log([, 1].findIndex(n => true)); //0
fill()
將一定范圍索引的數組元素內容填充為單個指定的值。
let arr = Array.of(1, 2, 3, 4); // 參數1:用來填充的值 // 參數2:被填充的起始索引 // 參數3(可選):被填充的結束索引,默認為數組末尾 console.log(arr.fill(0,1,2)); // [1, 0, 3, 4]
copyWithin()
將一定范圍索引的數組元素修改為此數組另一指定范圍索引的元素。
// 參數1:被修改的起始索引 // 參數2:被用來覆蓋的數據的起始索引 // 參數3(可選):被用來覆蓋的數據的結束索引,默認為數組末尾 console.log([1, 2, 3, 4].copyWithin(0,2,4)); // [3, 4, 3, 4] // 參數1為負數表示倒數 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"
// 1 "b"
// 不使用 for... of 循環(huán)
let entries = ['a', 'b'].entries();
console.log(entries.next().value); // [0, "a"]
console.log(entries.next().value); // [1, "b"]
// 數組含空位
console.log([...[,'a'].entries()]); // [[0, undefined], [1, "a"]]keys()
遍歷鍵名。
for(let key of ['a', 'b'].keys()){
console.log(key);
}
// 0
// 1
// 數組含空位
console.log([...[,'a'].keys()]); // [0, 1]values()
遍歷鍵值。
for(let value of ['a', 'b'].values()){
console.log(value);
}
// "a"
// "b"
// 數組含空位
console.log([...[,'a'].values()]); // [undefined, "a"]
includes()
數組是否包含指定值。
注意:與 Set 和 Map 的 has 方法區(qū)分;Set 的 has 方法用于查找值;Map 的 has 方法用于查找鍵名。
// 參數1:包含的指定值 [1, 2, 3].includes(1); // true // 參數2:可選,搜索的起始索引,默認為0 [1, 2, 3].includes(1, 2); // false // NaN 的包含判斷 [1, NaN, 3].includes(NaN); // true
flat()
console.log([1 ,[2, 3]].flat()); // [1, 2, 3] // 指定轉換的嵌套層數 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()
先對數組中每個元素進行了的處理,再對數組執(zhí)行 flat() 方法。
// 參數1:遍歷函數,該遍歷函數可接受3個參數:當前元素、當前元素索引、原數組 // 參數2:指定遍歷函數中 this 的指向 console.log([1, 2, 3].flatMap(n => [n * 2])); // [2, 4, 6]
數組緩沖區(qū)是內存中的一段地址。
定型數組的基礎。
實際字節(jié)數在創(chuàng)建時確定,之后只可修改其中的數據,不可修改大小。
通過構造函數創(chuàng)建:
let buffer = new ArrayBuffer(10); console.log(buffer.byteLength); // 10 分割已有數組緩沖區(qū) let buffer = new ArrayBuffer(10); let buffer1 = buffer.slice(1, 3); console.log(buffer1.byteLength); // 2
視圖是用來操作內存的接口。
視圖可以操作數組緩沖區(qū)或緩沖區(qū)字節(jié)的子集,并按照其中一種數值數據類型來讀取和寫入數據。
DataView 類型是一種通用的數組緩沖區(qū)視圖,其支持所有8種數值型數據類型。
創(chuàng)建:
// 默認 DataView 可操作數組緩沖區(qū)全部內容 let buffer = new ArrayBuffer(10); dataView = new DataView(buffer); dataView.setInt8(0,1); console.log(dataView.getInt8(0)); // 1 // 通過設定偏移量(參數2)與長度(參數3)指定 DataView 可操作的字節(jié)范圍 let buffer1 = new ArrayBuffer(10); dataView1 = new DataView(buffer1, 0, 3); dataView1.setInt8(5,1); // RangeError
數組緩沖區(qū)的特定類型的視圖。
可以強制使用特定的數據類型,而不是使用通用的 DataView 對象來操作數組緩沖區(qū)。
通過數組緩沖區(qū)生成
let buffer = new ArrayBuffer(10), view = new Int8Array(buffer); console.log(view.byteLength); // 10
通過構造函數
let view = new Int32Array(10);
console.log(view.byteLength); // 40
console.log(view.length); // 10
// 不傳參則默認長度為0
// 在這種情況下數組緩沖區(qū)分配不到空間,創(chuàng)建的定型數組不能用來保存數據
let view1 = new Int32Array();
console.log(view1.byteLength); // 0
console.log(view1.length); // 0
// 可接受參數包括定型數組、可迭代對象、數組、類數組對象
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 屬性不可寫,如果嘗試修改這個值,在非嚴格模式下會直接忽略該操作,在嚴格模式下會拋出錯誤。
let view = new Int16Array([1, 2]); view.length = 3; console.log(view.length); // 2
定型數組可使用 entries()、keys()、values()進行迭代。
let view = new Int16Array([1, 2]);
for(let [k, v] of view.entries()){
console.log(k, v);
}
// 0 1
// 1 2find() 等方法也可用于定型數組,但是定型數組中的方法會額外檢查數值類型是否安全,也會通過 Symbol.species 確認方法的返回值是定型數組而非普通數組。concat() 方法由于兩個定型數組合并結果不確定,故不能用于定型數組;另外,由于定型數組的尺寸不可更改,可以改變數組的尺寸的方法,例如 splice() ,不適用于定型數組。
let view = new Int16Array([1, 2]); view.find((n) > 1); // 2
所有定型數組都含有靜態(tài) of() 方法和 from() 方法,運行效果分別與 Array.of() 方法和 Array.from() 方法相似,區(qū)別是定型數組的方法返回定型數組,而普通數組的方法返回普通數組。
let view = Int16Array.of(1, 2); console.log(view instanceof Int16Array); // true
定型數組不是普通數組,不繼承自 Array 。
let view = new Int16Array([1, 2]); console.log(Array.isArray(view)); // false
定型數組中增加了 set() 與 subarray() 方法。 set() 方法用于將其他數組復制到已有定型數組, subarray() 用于提取已有定型數組的一部分形成新的定型數組。
// set 方法 // 參數1:一個定型數組或普通數組 // 參數2:可選,偏移量,開始插入數據的位置,默認為0 let view= new Int16Array(4); view.set([1, 2]); view.set([3, 4], 2); console.log(view); // [1, 2, 3, 4] // subarray 方法 // 參數1:可選,開始位置 // 參數2:可選,結束位置(不包含結束位置) 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]
let arr = [1, 2], arr1 = [...arr]; console.log(arr1); // [1, 2] // 數組含空位 let arr2 = [1, , 3], arr3 = [...arr2]; console.log(arr3); [1, undefined, 3]
合并數組
console.log([...[1, 2],...[3, 4]]); // [1, 2, 3, 4]
原文地址: https://www.linuxprobe.com/es6-array-introduction.html
網頁名稱:ES6數組介紹
網頁地址:http://chinadenli.net/article28/goecjp.html
成都網站建設公司_創(chuàng)新互聯(lián),為您提供面包屑導航、域名注冊、全網營銷推廣、品牌網站設計、網站內鏈、網站收錄
聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)