一、擴展運算符的應(yīng)用
ES6通過擴展元素符…,好比 rest 參數(shù)的逆運算,將一個數(shù)組轉(zhuǎn)為用逗號分隔的參數(shù)序列
console.log(...[1, 2, 3])
// 1 2 3
console.log(1, ...[2, 3, 4], 5)
// 1 2 3 4 5
[...document.querySelectorAll('div')]
// [<div>, <div>, <div>]
主要用于函數(shù)調(diào)用的時候,將一個數(shù)組變?yōu)閰?shù)序列
function push(array, ...items) {
array.push(...items);
}
function add(x, y) {
return x + y;
}
const numbers = [4, 38];
add(...numbers) // 42
可以將某些數(shù)據(jù)結(jié)構(gòu)轉(zhuǎn)為數(shù)組
[...document.querySelectorAll('div')]
能夠更簡單實現(xiàn)數(shù)組復(fù)制
const a1 = [1, 2];
const […a2] = a1;
// [1,2]
數(shù)組的合并也更為簡潔了
const arr1 = ['a', 'b'];
const arr2 = ['c'];
const arr3 = ['d', 'e'];
[...arr1, ...arr2, ...arr3]
// [ 'a', 'b', 'c', 'd', 'e' ]
注意:通過擴展運算符實現(xiàn)的是淺拷貝,修改了引用指向的值,會同步反映到新數(shù)組
下面看個例子就清楚多了
const arr1 = ['a', 'b',[1,2]];
const arr2 = ['c'];
const arr3 = [...arr1,...arr2]
arr1[2][0] = 9999 // 修改arr1里面數(shù)組成員值
console.log(arr3) // 影響到arr3,['a','b',[9999,2],'c']
擴展運算符可以與解構(gòu)賦值結(jié)合起來,用于生成數(shù)組
const [first, ...rest] = [1, 2, 3, 4, 5];
first // 1
rest // [2, 3, 4, 5]
const [first, ...rest] = [];
first // undefined
rest // []
const [first, ...rest] = ["foo"];
first // "foo"
rest // []
如果將擴展運算符用于數(shù)組賦值,只能放在參數(shù)的最后一位,否則會報錯
const [...butLast, last] = [1, 2, 3, 4, 5];
// 報錯
const [first, ...middle, last] = [1, 2, 3, 4, 5];
// 報錯
可以將字符串轉(zhuǎn)為真正的數(shù)組
[...'hello']
// [ "h", "e", "l", "l", "o" ]
定義了遍歷器(Iterator)接口的對象,都可以用擴展運算符轉(zhuǎn)為真正的數(shù)組
let nodeList = document.querySelectorAll('div');
let array = [...nodeList];
let map = new Map([
[1, 'one'],
[2, 'two'],
[3, 'three'],
]);
let arr = [...map.keys()]; // [1, 2, 3]
如果對沒有 Iterator 接口的對象,使用擴展運算符,將會報錯
const obj = {a: 1, b: 2};
let arr = [...obj]; // TypeError: Cannot spread non-iterable object
二、構(gòu)造函數(shù)新增的方法
關(guān)于構(gòu)造函數(shù),數(shù)組新增的方法有如下:
- Array.from()
- Array.of()
Array.from()
將兩類對象轉(zhuǎn)為真正的數(shù)組:類似數(shù)組的對象和可遍歷(iterable)的對象(包括 ES6 新增的數(shù)據(jù)結(jié)構(gòu) Set 和 Map)
let arrayLike = {
'0': 'a',
'1': 'b',
'2': 'c',
length: 3
};
let arr2 = Array.from(arrayLike); // ['a', 'b', 'c']
如果是對象,必須有l(wèi)ength
還可以接受第二個參數(shù),用來對每個元素進行處理,將處理后的值放入返回的數(shù)組
Array.from([1, 2, 3], (x) => x * x)
// [1, 4, 9]
Array.of()
用于將一組值,轉(zhuǎn)換為數(shù)組
Array.of(3, 11, 8) // [3,11,8]
沒有參數(shù)的時候,返回一個空數(shù)組
當(dāng)參數(shù)只有一個的時候,實際上是指定數(shù)組的長度
參數(shù)個數(shù)不少于 2 個時,Array()才會返回由參數(shù)組成的新數(shù)組
Array() // []
Array(3) // [, , ,]
Array(3, 11, 8) // [3, 11, 8]
三、實例對象新增的方法
關(guān)于數(shù)組實例對象新增的方法有如下:
- copyWithin()
- find()、findIndex()
- fill()
- entries(),keys(),values()
- includes()
- flat(),flatMap()
copyWithin()
將指定位置的成員復(fù)制到其他位置(會覆蓋原有成員),然后返回當(dāng)前數(shù)組
參數(shù)如下:
- target(必需):從該位置開始替換數(shù)據(jù)。如果為負值,表示倒數(shù)。
- start(可選):從該位置開始讀取數(shù)據(jù),默認(rèn)為 0。如果為負值,表示從末尾開始計算。
- end(可選):到該位置前停止讀取數(shù)據(jù),默認(rèn)等于數(shù)組長度。如果為負值,表示從末尾開始計算。
[1, 2, 3, 4, 5].copyWithin(0, 3) // 將從 3 號位直到數(shù)組結(jié)束的成員(4 和 5),復(fù)制到從 0 號位開始的位置,結(jié)果覆蓋了原來的 1 和 2
// [4, 5, 3, 4, 5]
find()、findIndex()
find()用于找出第一個符合條件的數(shù)組成員
參數(shù)是一個回調(diào)函數(shù),接受三個參數(shù)依次為當(dāng)前的值、當(dāng)前的位置和原數(shù)組
[1, 5, 10, 15].find(function(value, index, arr) {
return value > 9;
}) // 10
findIndex返回第一個符合條件的數(shù)組成員的位置,如果所有成員都不符合條件,則返回-1
[1, 5, 10, 15].findIndex(function(value, index, arr) {
return value > 9;
}) // 2
這兩個方法都可以接受第二個參數(shù),用來綁定回調(diào)函數(shù)的this對象。
function f(v){
return v > this.age;
}
let person = {name: 'John', age: 20};
[10, 12, 26, 15].find(f, person); // 26
fill()
使用給定值,填充一個數(shù)組
['a', 'b', 'c'].fill(7)
// [7, 7, 7]
new Array(3).fill(7)
// [7, 7, 7]
還可以接受第二個和第三個參數(shù),用于指定填充的起始位置和結(jié)束位置
['a', 'b', 'c'].fill(7, 1, 2)
// ['a', 7, 'c']
注意,如果填充的類型為對象,則是淺拷貝
entries(),keys(),values()
keys()是對鍵名的遍歷、values()是對鍵值的遍歷,entries()是對鍵值對的遍歷
for (let index of ['a', 'b'].keys()) {
console.log(index);
}
// 0
// 1
for (let elem of ['a', 'b'].values()) {
console.log(elem);
}
// 'a'
// 'b'
for (let [index, elem] of ['a', 'b'].entries()) {
console.log(index, elem);
}
// 0 "a"
includes()
用于判斷數(shù)組是否包含給定的值
[1, 2, 3].includes(2) // true
[1, 2, 3].includes(4) // false
[1, 2, NaN].includes(NaN) // true
方法的第二個參數(shù)表示搜索的起始位置,默認(rèn)為0
參數(shù)為負數(shù)則表示倒數(shù)的位置
[1, 2, 3].includes(3, 3); // false
[1, 2, 3].includes(3, -1); // true
flat(),flatMap()
將數(shù)組扁平化處理,返回一個新數(shù)組,對原數(shù)據(jù)沒有影響
[1, 2, [3, 4]].flat()
// [1, 2, 3, 4]
flat()默認(rèn)只會“拉平”一層,如果想要“拉平”多層的嵌套數(shù)組,可以將flat()方法的參數(shù)寫成一個整數(shù),表示想要拉平的層數(shù),默認(rèn)為1
[1, 2, [3, [4, 5]]].flat()
// [1, 2, 3, [4, 5]]
[1, 2, [3, [4, 5]]].flat(2)
// [1, 2, 3, 4, 5]
flatMap()方法對原數(shù)組的每個成員執(zhí)行一個函數(shù)相當(dāng)于執(zhí)行Array.prototype.map(),然后對返回值組成的數(shù)組執(zhí)行flat()方法。該方法返回一個新數(shù)組,不改變原數(shù)組
// 相當(dāng)于 [[2, 4], [3, 6], [4, 8]].flat()
[2, 3, 4].flatMap((x) => [x, x * 2])
// [2, 4, 3, 6, 4, 8]
flatMap()方法還可以有第二個參數(shù),用來綁定遍歷函數(shù)里面的this
四、數(shù)組的空位
數(shù)組的空位指,數(shù)組的某一個位置沒有任何值
ES6 則是明確將空位轉(zhuǎn)為undefined,包括Array.from、擴展運算符、copyWithin()、fill()、entries()、keys()、values()、find()和findIndex()
建議大家在日常書寫中,避免出現(xiàn)空位
五、排序穩(wěn)定性
將sort()默認(rèn)設(shè)置為穩(wěn)定的排序算法文章來源:http://www.zghlxwxcb.cn/news/detail-601559.html
const arr = [
'peach',
'straw',
'apple',
'spork'
];
const stableSorting = (s1, s2) => {
if (s1[0] < s2[0]) return -1;
return 1;
};
arr.sort(stableSorting)
// ["apple", "peach", "straw", "spork"]
排序結(jié)果中,straw在spork的前面,跟原始順序一致文章來源地址http://www.zghlxwxcb.cn/news/detail-601559.html
到了這里,關(guān)于ES6基礎(chǔ)知識二:ES6中數(shù)組新增了哪些擴展?的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!