1. Array
- Array.of() 方法創(chuàng)建一個(gè)具有可變數(shù)量參數(shù)的新數(shù)組實(shí)例逻住,而不考慮參數(shù)的數(shù)量或類型,返回該數(shù)組菲嘴。
Array.of(7) 創(chuàng)建一個(gè)具有單個(gè)元素 7 的數(shù)組,而 Array(7) 創(chuàng)建一個(gè)包含 7 個(gè) undefined 元素的數(shù)組腕唧。
Array.of(7); // [7]
Array.of(1, 2, 3); // [1, 2, 3]
Array(7); // [ , , , , , , ]
Array(1, 2, 3); // [1, 2, 3]
- Array.from() 方法從一個(gè)類似數(shù)組或可迭代對(duì)象中創(chuàng)建一個(gè)新的數(shù)組實(shí)例蹋订。
Array.from()
可以通過以下方式來創(chuàng)建數(shù)組對(duì)象:
偽數(shù)組對(duì)象(擁有一個(gè) length 屬性和若干索引屬性的任意對(duì)象),例如:字符串
可迭代對(duì)象(可以獲取對(duì)象中的元素,如 Map和 Set 等);
const bar = ["a", "b", "c"];
Array.from(bar);
// ["a", "b", "c"]
Array.from('foo');
// ["f", "o", "o"]
Array.from()
方法有一個(gè)可選參數(shù) mapFn
者填,讓你可以在最后生成的數(shù)組上再執(zhí)行一次 map
方法后再返回。也就是說Array.from(obj, mapFn, thisArg)
就相當(dāng)于Array.from(obj).map(mapFn, thisArg),
除非創(chuàng)建的不是可用的中間數(shù)組。
let m = new Map([[1, 2], [2, 4], [4, 8]]);
Array.from(m);
// [[1, 2], [2, 4], [4, 8]]
- prototype.every
every() 方法測(cè)試數(shù)組的所有元素是否都通過了指定函數(shù)的測(cè)試颤陶。every 方法為數(shù)組中的每個(gè)元素執(zhí)行一次 callback 函數(shù)颗管,直到它找到一個(gè)使 callback 返回 false(表示可轉(zhuǎn)換為布爾值 false 的值)的元素。如果發(fā)現(xiàn)了一個(gè)這樣的元素滓走,every 方法將會(huì)立即返回 false垦江。否則,callback 為每一個(gè)元素返回 true搅方,every 就會(huì)返回 true比吭。
function isBigEnough(element, index, array) {
return (element >= 10);
}
var passed = [12, 5, 8, 130, 44].every(isBigEnough);
// passed is false
passed = [12, 54, 18, 130, 44].every(isBigEnough);
// passed is true
- prototype. some
some() 方法測(cè)試數(shù)組中的某些元素是否通過由提供的函數(shù)實(shí)現(xiàn)的測(cè)試。
const isBiggerThan10 = (element, index, array) => {
return element > 10;
}
[2, 5, 8, 1, 4].some(isBiggerThan10);
// false
[12, 5, 8, 1, 4].some(isBiggerThan10);
// true
- prototype.find
arr.find(callback[, thisArg])
find()方法返回?cái)?shù)組中滿足提供的測(cè)試函數(shù)的第一個(gè)元素的值姨涡。否則返回 undefined.callback與some,foreach,every,some一樣的參數(shù)
function isBigEnough(element) {
return element >= 15;
}
[12, 5, 8, 130, 44].find(isBigEnough); // 130
在第一次調(diào)用 callback 函數(shù)時(shí)會(huì)確定元素的索引范圍衩藤,因此在 find 方法開始執(zhí)行之后添加到數(shù)組的新元素將不會(huì)被 callback 函數(shù)訪問到。如果數(shù)組中一個(gè)尚未被callback函數(shù)訪問到的元素的值被callback函數(shù)所改變涛漂,那么當(dāng)callback函數(shù)訪問到它時(shí)赏表,它的值是將是根據(jù)它在數(shù)組中的索引所訪問到的當(dāng)前值。被刪除的元素仍舊會(huì)被訪問到匈仗。
- prototype.findIndex
arr.findIndex(callback[, thisArg])
findIndex()方法返回?cái)?shù)組中滿足提供的測(cè)試函數(shù)的第一個(gè)元素的索引瓢剿。否則返回-1。
function isBigEnough(element) {
return element >= 15;
}
[12, 5, 8, 130, 44].findIndex(isBigEnough);
// index of 4th element in the Array is returned,
// so this will result in '3'
- prototype.fill
arr.fill(value, start, end)
value:用來填充數(shù)組元素的值悠轩。
start 可選:起始索引间狂,默認(rèn)值為0。
end 可選:終止索引火架,默認(rèn)值為 this.length鉴象。
返回值:修改后的數(shù)組。
fill() 方法用一個(gè)固定值填充一個(gè)數(shù)組中從起始索引到終止索引內(nèi)的全部元素何鸡。
[1, 2, 3].fill(4) // [4, 4, 4]
[1, 2, 3].fill(4, 1) // [1, 4, 4]
[1, 2, 3].fill(4, 1, 2) // [1, 4, 3]
[1, 2, 3].fill(4, 1, 1) // [1, 2, 3]
[1, 2, 3].fill(4, -3, -2) // [4, 2, 3]
[1, 2, 3].fill(4, NaN, NaN) // [1, 2, 3]
Array(3).fill(4); // [4, 4, 4]
[].fill.call({length: 3}, 4) // {0: 4, 1: 4, 2: 4, length: 3}
- prototype.includes
includes() 方法用來判斷一個(gè)數(shù)組是否包含一個(gè)指定的值纺弊,根據(jù)情況,如果包含則返回 true骡男,否則返回false俭尖。
arr.includes(searchElement, fromIndex )
searchElement:需要查找的元素值。
fromIndex 可選:從該索引處開始查找 searchElement洞翩。如果為負(fù)值稽犁,則按升序從 array.length + fromIndex 的索引開始搜索。默認(rèn)為 0骚亿。
let a = [1, 2, 3];
a.includes(2);
// true
a.includes(4);
// false
- prototype.map
創(chuàng)建一個(gè)新數(shù)組已亥,其結(jié)果是該數(shù)組中的每個(gè)元素都調(diào)用一個(gè)提供的函數(shù)后返回的結(jié)果
// ES6
let numbers = [1, 5, 10, 15];
let doubles = numbers.map( x => x ** 2);
// doubles is now [1, 25, 100, 225]
// numbers is still [1, 5, 10, 15]
const numbers = [2, 4, 8, 10];
let halves = numbers.map(x => x / 2);
let numbers = [1, 4, 9];
let roots = numbers.map(Math.sqrt);
// roots is now [1, 2, 3]
// numbers is still [1, 4, 9]
- prototype.reduce
對(duì)累加器和數(shù)組中的每個(gè)元素(從左到右)應(yīng)用一個(gè)函數(shù),將其減少為單個(gè)值来屠。
arr.reduce(callback[, initialValue])
callback
執(zhí)行數(shù)組中每個(gè)值的函數(shù)虑椎,包含四個(gè)參數(shù):- accumulator
累加器累加回調(diào)的返回值; 它是上一次調(diào)用回調(diào)時(shí)返回的累積值震鹉,或initialValue(如下所示)。 - currentValue
數(shù)組中正在處理的元素捆姜。 - currentIndex
數(shù)組中正在處理的當(dāng)前元素的索引传趾。 如果提供了initialValue,則索引號(hào)為0泥技,否則為索引為1浆兰。 - array
調(diào)用reduce的數(shù)組
- accumulator
var total = [0, 1, 2, 3].reduce(function(sum, value) {
return sum + value;
}, 0);
// total is 6
var flattened = [[0, 1], [2, 3], [4, 5]].reduce(function(a, b) {
return a.concat(b);
}, []);
// flattened is [0, 1, 2, 3, 4, 5]