擴展運算符
擴展運算符(spread)是三個點(...)
console.log(...[1, 2, 3]); // 1 2 3
function add (x,y,z) {
return x+y+z
}
add([...[1,2,3]) // 6
作用
- 復(fù)制數(shù)組
const a1 = [1, 2];
const a2 = a1;
a2[0] = 2;
a1; // [2, 2]
上面代碼中,a2 并不是 a1 的克隆斜做,而是指向同一份數(shù)據(jù)的另一個指針趁尼。修改 a2,會直接導(dǎo)致 a1 的變化催蝗。
const a1 = [1, 2];
// 寫法一
const a2 = [...a1];
// 寫法二
const [...a2] = a1;
- 合并數(shù)組
let arr1 = [1, 2];
let arr2 = [2, 3];
let arr3 = [...arr1, ...arr2]; // [1 ,2 ,2, 3]
本質(zhì)上(...) 會調(diào)用數(shù)組的 Iterator ,所以只要存在 Iterator 的數(shù)據(jù)都可以被擴展
let arr1 = [...5]; // Uncaught TypeError: number 5 is not iterable (cannot read property Symbol(Symbol.iterator))
數(shù)字 5 不能被擴展的原因是 cannot read property Symbol(Symbol.iterator),所以如果我們給他定義了這個方法育特,那么就可以被擴展了
Number.prototype[Symbol.iterator] = function() {
let i = 0;
let num = this.valueOf();
return {
next: () => {
return i < num ? { value: 2 * i++ } : { done: true };
}
};
};
console.log([...6]); // [0, 2, 4, 6, 8, 10]
當(dāng)然這種方式在平時工作中慎用丙号,不然會讓人覺得莫名其妙
所有可以 Iterator 的數(shù)據(jù)都可以被擴展成數(shù)組
Array api
type | 作用 |
---|---|
Array.from(Array, cb, this) | 將類數(shù)組和可遍歷的對象轉(zhuǎn)為真正的數(shù)組 |
Array.of() | 將一組值,轉(zhuǎn)換為數(shù)組缰冤。 |
// 還是用上面的例子
Number.prototype[Symbol.iterator] = function() {
let i = 0;
let num = this.valueOf();
return {
next: () => {
return i < num ? { value: 2 * i++ } : { done: true };
}
};
};
[...6]; // [0, 2, 4, 6, 8, 10]
Array.from(6); // [0, 2, 4, 6, 8, 10]
for (let v of 6) {
console.log(6);
}
// 0 2 4 6 8 10
Array.from(6, v => v + 10); // [10, 12, 14, 16, 18, 20]
Array.from(
6,
function(v) {
return this.valueOf() + v;
},
6
);
Array.of(1, 2); // [1, 2]
數(shù)組實例的 API
type | 作用 | 返回值 | 是否修改原數(shù)組 |
---|---|---|---|
copyWithin(target, start = 0, end = this.length) | 在當(dāng)前數(shù)組內(nèi)部犬缨,將指定位置的成員復(fù)制到其他位置(會覆蓋原有成員),然后返回當(dāng)前數(shù)組棉浸。也就是說怀薛,使用這個方法,會修改當(dāng)前數(shù)組迷郑。 target(必需):從該位置開始替換數(shù)據(jù)枝恋。如果為負值,表示倒數(shù)嗡害。start(可選):從該位置開始讀取數(shù)據(jù)焚碌,默認為 0。如果為負值霸妹,表示倒數(shù)十电。end(可選):到該位置前停止讀取數(shù)據(jù),默認等于數(shù)組長度。如果為負值摆出,表示倒數(shù)朗徊。 | 新的數(shù)組 | 是 |
find((value, index, arr)=>{}, this ) | 用于找出第一個符合條件的數(shù)組成員 | 返回找到的數(shù)組成員 , 沒找到就返回 undefined | 否 |
findIndex((value, index, arr)=>{}, this ) | 用于找出第一個符合條件的數(shù)組成員 | 返回第一個符合條件的數(shù)組成員的位置偎漫,如果所有成員都不符合條件爷恳,則返回-1 | 否 |
fill(value, startIndex, endIndex) | 使用給定值,填充一個數(shù)組象踊。value (可選):填充值温亲。startIndex (可選):開始填充的位置。endIndex (可選):結(jié)束填充的位置杯矩。 注意栈虚,如果填充的類型為對象,那么被賦值的是同一個內(nèi)存地址的對象史隆,而不是深拷貝對象魂务。 | 填充后的新數(shù)組 | 是 |
includes(value,index) | 表示某個數(shù)組是否包含給定的值,用來替代 indexOf | 返回布爾值,true/false | 否 |
flat(n=1) | 用來給數(shù)組降維。n 表示降低的維數(shù)泌射,n 也可以是 Infinity 粘姜,表示無論有多少維,都降成一維 | 返回降維后的數(shù)組 | 是 |
flatMap((value,index,array) => {},n) | 等同于先對數(shù)組執(zhí)行 map() ,然后再執(zhí)行 flat() | 返回新的數(shù)組 | 是 |
entries() | 對鍵值對的遍歷 | 遍歷器對象 | 否 |
keys() | 對鍵名的遍歷 | 遍歷器對象 | 否 |
values() | 對鍵值的遍歷 | 遍歷器對象 | 否 |
例子
// 將3號位復(fù)制到0號位
[1, 2, 3, 4, 5].copyWithin(0, 3, 4)
// [4, 2, 3, 4, 5]
// -2相當(dāng)于3號位熔酷,-1相當(dāng)于4號位
[1, 2, 3, 4, 5].copyWithin(0, -2, -1)
// [4, 2, 3, 4, 5]
// 將3號位復(fù)制到0號位
[].copyWithin.call({length: 5, 3: 1}, 0, 3)
// {0: 1, 3: 1, length: 5}
// 將2號位到數(shù)組結(jié)束孤紧,復(fù)制到0號位
let i32a = new Int32Array([1, 2, 3, 4, 5]);
i32a.copyWithin(0, 2);
// Int32Array [3, 4, 5, 4, 5]
// 對于沒有部署 TypedArray 的 copyWithin 方法的平臺
// 需要采用下面的寫法
[].copyWithin.call(new Int32Array([1, 2, 3, 4, 5]), 0, 3, 4);
// Int32Array [4, 2, 3, 4, 5]
[1, 4, -5, 10].find(n => n < 0);
// -5
[1, 5, 10, 15].find(function(value, index, arr) {
return value > 9;
}); // 10
[1, 5, 10, 15].findIndex(function(value, index, arr) {
return value > 9;
}); // 2
function f(v) {
return v > this.age;
}
let person = { name: "John", age: 20 };
[10, 12, 26, 15].find(f, person); // 26
// 另外,這兩個方法都可以發(fā)現(xiàn)NaN拒秘,彌補了數(shù)組的indexOf方法的不足号显。
[NaN]
.indexOf(NaN)
// -1
[NaN].findIndex(y => Object.is(NaN, y));
// 0
["a", "b", "c"].fill(7);
// [7, 7, 7]
new Array(3).fill(7);
// [7, 7, 7]
["a", "b", "c"].fill(7, 1, 2);
// ['a', 7, 'c']
let arr = new Array(3).fill({ name: "Mike" });
arr[0].name = "Ben";
arr;
// [{name: "Ben"}, {name: "Ben"}, {name: "Ben"}]
let arr = new Array(3).fill([]);
arr[0].push(5);
arr;
// [[5], [5], [5]]
數(shù)組的空位
ES5
type | api |
---|---|
跳過空位 | forEach(), filter(), reduce(), every() 和 some() |
跳過空位,但會保留這個值 | map() |
視為 undefined | join()和 toString() |
ES6
明確將空位處理成 undefined躺酒,這句話的意思就是es6 之后的遍歷方法 都會把空位處理成 undefined
回到目錄