@(JavaScript基礎(chǔ))
關(guān)于構(gòu)造函數(shù)Array屬性和方法總結(jié)
構(gòu)造函數(shù)Array屬性:
Array.length
Array 構(gòu)造函數(shù)的 length 屬性础倍,其值為1。
Array.prototype
構(gòu)造函數(shù)Array方法
Array.isArray
Array.isArray(obj):
描述:
假如一個變量是數(shù)組則返回true衅澈,否則返回false
參數(shù):
obj
返回值:
boolean值
例子:
// 下面的函數(shù)調(diào)用都返回 true
Array.isArray([]);
Array.isArray([1]);
Array.isArray(new Array());
// 鮮為人知的事實:其實 Array.prototype 也是一個數(shù)組。
Array.isArray(Array.prototype);
// 下面的函數(shù)調(diào)用都返回 false
Array.isArray();
Array.isArray({});
Array.isArray(null);
Array.isArray(undefined);
Array.isArray(17);
Array.isArray('Array');
Array.isArray(true);
Array.isArray(false);
Array.isArray({ __proto__: Array.prototype })
Array.of
Array.of(element0[, element1[, ...[, elementN]]]):
描述:
方法創(chuàng)建一個具有可變數(shù)量參數(shù)的新數(shù)組實例,而不考慮參數(shù)的數(shù)量或類型
參數(shù):
elementN
任意個參數(shù)爽室,將按順序成為返回數(shù)組中的元素神年。
返回值
新的 Array 實例
例子:
Array.of(1); // [1]
Array.of(1, 2, 3); // [1, 2, 3]
Array.from
Array.from(arrayLike, mapFn, thisArg):
描述:
從類數(shù)組或者迭代對象(iterable object)中創(chuàng)建一個新的數(shù)組實例,
Array.from(obj, mapFn, thisArg) 就相當(dāng)于 Array.from(obj).map(mapFn, thisArg)
偽數(shù)組對象(擁有一個 length 屬性和若干索引屬性的任意對象)
可迭代對象(可以獲取對象中的元素,如 Map和 Set 等)
參數(shù):
arrayLike
想要轉(zhuǎn)換成數(shù)組的偽數(shù)組對象或可迭代對象已维。
mapFn (可選參數(shù))
如果指定了該參數(shù),新數(shù)組中的每個元素會執(zhí)行該回調(diào)函數(shù)已日。
thisArg (可選參數(shù))
可選參數(shù)垛耳,執(zhí)行回調(diào)函數(shù) mapFn 時 this 對象
返回值:
一個新的數(shù)組實例
例子:
Array.from('foo'); // ["f", "o", "o"]
const obj={
name:'lee',
};
const forEach=item=>item+=item;
const arrayLike={
0:'name',
1:'age',
length:2
}
Array.from(arrayLike,forEach,obj); // ["namename", "ageage"]
構(gòu)造函數(shù)Array實例方法
數(shù)組的方法也可以分為增刪改查:
查找:
// 以下方法會返回該字符串是否存在某特定字符串的boolean值
----------------------------------------------------------------------------
Array.prototype.includes(searchElement, fromIndex)
描述:
判斷當(dāng)前數(shù)組是否包含某指定的值,如果是返回 true飘千,否則返回 false
參數(shù):
searchElement:
需要查找的元素值堂鲜。
fromIndex:
從該索引處開始查找 searchElement
返回值:
boolean值
例子:
[1, 2, 3].includes(2); // true
[1, 2, 3].includes(3, 2); // true
----------------------------------------------------------------------------
// 以下方法會返回某元素在數(shù)組中的索引
----------------------------------------------------------------------------
Array.prototype.indexOf(searchElement[, fromIndex = 0]):
描述:
返回數(shù)組中第一個與指定值相等的元素的索引,如果找不到這樣的元素护奈,則返回 -1
參數(shù):
searchElement:
要查找的元素
fromIndex:
開始查找的位置
返回值:
元素所在的索引
例子:
const arr=[1,2,3,4,5,6];
arr.indexOf(5); // 4
arr.indexOf(6,3); // 5
----------------------------------------------------------------------------
Array.prototype.lastIndexOf(searchElement[, fromIndex = arr.length-1]):
描述:
返回數(shù)組中第一個與指定值相等的元素的索引缔莲,如果找不到這樣的元素,則返回 -1
參數(shù):
searchElement:
要查找的元素
fromIndex:
開始查找的位置
返回值:
元素所在的索引
例子:
const arr=[1,2,3,4,5,6];
arr.lastIndexOf(5); // 4
arr.lastIndexOf(2,3); // 1
arr.lastIndexOf(6,3); // -1
----------------------------------------------------------------------------
Array.prototype.findIndex(callback,thisArg):
描述:
找到第一個滿足測試函數(shù)的元素并返回那個元素的索引霉旗,如果找不到痴奏,則返回 -1
參數(shù):
callback
針對數(shù)組中的每個元素, 都會執(zhí)行該回調(diào)函數(shù), 執(zhí)行時會自動傳入下面三個參數(shù):
element
當(dāng)前元素。
index
當(dāng)前元素的索引厌秒。
array
調(diào)用findIndex的數(shù)組读拆。
返回值:
強制為true的某元素
thisArg
執(zhí)行callback時作為this對象的值
返回值:
滿足回調(diào)函數(shù)元素的索引
例子:
const foo=(item,index)=>{
if(item%2===0){
return false;
}else{
return item>10;
}
}
[6,9,10,11].findIndex(foo); // 3
[6,9,10,20].findIndex(foo); // -1
----------------------------------------------------------------------------
// 以下方法會返回數(shù)組中的某元素
----------------------------------------------------------------------------
Array.prototype.find(callback[, thisArg])
描述:
找到第一個滿足測試函數(shù)的元素并返回那個元素的值,如果找不到鸵闪,則返回 undefined
參數(shù):
callback
在數(shù)組每一項上執(zhí)行的函數(shù)檐晕,接收 3 個參數(shù):
element
當(dāng)前遍歷到的元素。
index
當(dāng)前遍歷到的索引岛马。
array
數(shù)組本身
返回值:
強制為true的某元素
thisArg 可選
指定 callback 的 this 參數(shù)
返回值:
當(dāng)某個元素通過 callback 的測試時棉姐,返回數(shù)組中的一個值,否則返回 undefined
例子:
const foo=(item,index)=>{
if(item%2===0){
return false;
}else{
return item>10;
}
}
[6,9,10,11].find(foo); // 11
[6,20,10,20].find(foo); // undefined
----------------------------------------------------------------------------
增加:
// 該方法向數(shù)組開頭添加元素
----------------------------------------------------------------------------
Array.prototype.unshift(element1, ..., elementN):
描述:
將一個或多個元素添加到數(shù)組的開頭啦逆,并返回新數(shù)組的長度
參數(shù):
elementN
返回值:
新數(shù)組的長度
例子:
const arr=[1,2,3];
arr.unshift(0); // 4
const arr2=[1,2,3];
arr2.unshift(4,5,6); // 6
----------------------------------------------------------------------------
// 該方法向數(shù)組尾部添加元素
----------------------------------------------------------------------------
Array.prototype.push(element1, ..., elementN):
描述:
在數(shù)組的末尾增加一個或多個元素伞矩,并返回數(shù)組的新長度
參數(shù):
elementN
返回值:
新數(shù)組的長度
----------------------------------------------------------------------------
// 該方法用特定值填充數(shù)組特定位置
----------------------------------------------------------------------------
Array.prototype.fill(value, start, end):
描述:
用一個固定值填充一個數(shù)組中從起始索引到終止索引內(nèi)的全部元素
參數(shù):
value
start
end
返回值:
修改后的數(shù)組
例子:
[1,2,3,4].fill(5); // [5,5,5,5]
[1,2,3,4].fill(5,2); // [1,2,5,5]
[1,2,3,4].fill(5,1,4); // [1,5,5,5]
----------------------------------------------------------------------------
編輯
總結(jié):
splice() 方法與 slice() 方法的作用是不同的,
splice() 方法會直接對數(shù)組進行修改,
slice()方法會返回截取后的數(shù)組
----------------------------------------------------------------------------
Array.prototype.slice(begin,end)
描述:
抽取當(dāng)前數(shù)組中的一段元素組合成一個新數(shù)組
參數(shù):
begin:
提取數(shù)組的起始位置夏志,默認值為0
end:
提取數(shù)組的結(jié)束位置(該位置取不到)乃坤,默認值為數(shù)組長度
返回值:
一個含有提取元素的新數(shù)組
例子:
const names=['panda','cat','dog','monkey'];
names.slice(); // ["panda", "cat", "dog", "mokey"]
names.slice(1); // ["cat", "dog", "mokey"]
names.slice(1,3); // ['cat','dog']
----------------------------------------------------------------------------
Array.prototype.splice(start, deleteCount, item1, item2, ...):
描述:
通過刪除現(xiàn)有元素和/或添加新元素來更改一個數(shù)組的內(nèi)容
參數(shù):
start?:
指定修改的開始位置(從0計數(shù))
若只使用start參數(shù)而不使用deleteCount苛让、item,如:array.splice(start) 湿诊,
表示刪除[start狱杰,end]的元素
deleteCount:
整數(shù),表示要移除的數(shù)組元素的個數(shù)
item1, item2, ... :
要添加進數(shù)組的元素,從start 位置開始
返回值:
由被刪除的元素組成的一個數(shù)組厅须。
如果只刪除了一個元素仿畸,則返回只包含一個元素的數(shù)組。
如果沒有刪除元素朗和,則返回空數(shù)組
例子:
[1,2,3,4,5].splice(3); // [4,5]
[1,2,3,4,5].splice(2,3); // [3,4,5]
const arr=[1,2,3,4,5];
arr.splice(2,3,6);
// [3,4,5]
// arr: [1,2,6]
arr.splice(2,4,6,7,8,9,0);
// [3, 4, 5]
// [1, 2, 6, 7, 8, 9, 0]
----------------------------------------------------------------------------
// 該方法會對數(shù)組內(nèi)部元素進行復(fù)制
----------------------------------------------------------------------------
Array.prototype.copyWithin(target, start, end):
描述:
淺復(fù)制數(shù)組的一部分到同一數(shù)組中的另一個位置错沽,并返回它,而不修改其大小
參數(shù):
target:
0 為基底的索引眶拉,復(fù)制序列到該位置千埃。如果是負數(shù),target 將從末尾開始計算
start:
0 為基底的索引忆植,開始復(fù)制元素的起始位置放可。如果是負數(shù),start 將從末尾開始計算
如果 start 被忽略朝刊,copyWithin 將會從0開始復(fù)制
end:
0 為基底的索引耀里,開始復(fù)制元素的結(jié)束位置
如果 end 被忽略,copyWithin 將會復(fù)制到 arr.length
返回值:
改變了的數(shù)組
例子:
[1,2,3,4,5].copyWithin(-2); // [1,2,3,1,2]
[1,2,3,4,5].copyWithin(2,1); // [1,2,2,3,4]
[1,2,3,4,5].copyWithin(2,1,3); // [1,2,2,3,5]
----------------------------------------------------------------------------
// 該方法會反轉(zhuǎn)數(shù)組
----------------------------------------------------------------------------
Array.prototype.reverse():
描述:
顛倒數(shù)組中元素的排列順序拾氓,即原先的第一個變?yōu)樽詈笠粋€备韧,原先的最后一個變?yōu)榈谝粋€,然后依次顛倒
無參:
返回值:
顛倒順序之后的新數(shù)組
例子:
['hello','world','today','tomorrow','yesterday'].reverse();
// ["yesterday", "tomorrow", "today", "world", "hello"]
----------------------------------------------------------------------------
// 該方法會對數(shù)組進行排序
----------------------------------------------------------------------------
Array.prototype.sort(compareFunction)
描述:
在適當(dāng)?shù)奈恢脤?shù)組的元素進行排序,并返回數(shù)組痪枫。
默認排序順序是根據(jù)字符串Unicode碼點
參數(shù)
compareFunction
可選。用來指定按某種順序進行排列的函數(shù)叠艳。
如果省略奶陈,元素按照轉(zhuǎn)換為的字符串的諸個字符的Unicode位點進行排序
如果 compareFunction(a, b) 小于 0 ,那么 a 會被排列到 b 之前附较;
如果 compareFunction(a, b) 等于 0 吃粒, a 和 b 的相對位置不變
如果 compareFunction(a, b) 大于 0 , b 會被排列到 a 之前
返回值:
排序后的數(shù)組
例子:
['atom','diy','coo','bar'].sort(); // ["atom", "bar", "coo", "diy"]
function compare(val1,val2){
return val1-val2;
};
[2,9,4,7,5].sort(compare); // [2, 4, 5, 7, 9]
const items=[
{name:'lee',age:18},
{name:'panda',age:9},
{name:'cat',age:15},
{name:'dog',age:6},
{name:'monkey',age:20},
];
function compareAge(val1,val2){
return val1.age-val2.age;
};
items.sort(compareAge);
// [
// {name:'dog',age:6},
// {name:'panda',age:9},
// {name:'cat',age:15},
// {name:'lee',age:18},
// {name:'monkey',age:20},
// ]
----------------------------------------------------------------------------
// 合并數(shù)組
----------------------------------------------------------------------------
Array.prototype.concat(value1[, value2[, ...[, valueN]]]):
描述:
用于合并兩個或多個數(shù)組拒课。此方法不會更改現(xiàn)有數(shù)組徐勃,而是返回一個新數(shù)組
參數(shù):
valueN:
將數(shù)組和/或值連接成新數(shù)組
返回值:
新的 Array 實例
例子:
const arr1=[1,2,3],
arr2=[4,5,6],
arr3=[7,8,9];
arr1.concat(arr2,arr3);
// [1,2,3,4,5,6,7,8,9];
arr1.concat(0,arr2,arr3);
// [1,2,3,0,4,5,6,7,8,9]
----------------------------------------------------------------------------
刪除:
// 該方法刪除數(shù)組中第一個元素
----------------------------------------------------------------------------
Array.prototype.shift()
描述:
從數(shù)組中刪除第一個元素,并返回該元素的值早像。此方法更改數(shù)組的長度
無參:
返回值:
[1,2,3,4,5].shift(); // 1
----------------------------------------------------------------------------
// 該方法刪除數(shù)組中最后一個元素
----------------------------------------------------------------------------
Array.prototype.pop():
描述:
從數(shù)組中刪除最后一個元素僻肖,并返回該元素的值。此方法更改數(shù)組的長度
無參
返回值:
從數(shù)組中刪除的元素
例子:
[1,2,3,4].pop(); // 4
----------------------------------------------------------------------------
以下數(shù)組方法與字符串有關(guān)
----------------------------------------------------------------------------
Array.prototype.join(separator):
描述:
將數(shù)組(或一個類數(shù)組對象)的所有元素連接到一個字符串中
參數(shù)
separator:
指定一個字符串來分隔數(shù)組的每個元素卢鹦。
如果需要(separator)臀脏,將分隔符轉(zhuǎn)換為字符串。
如果省略(),數(shù)組元素用逗號分隔揉稚。默認為 ","秒啦。
如果separator是空字符串(""),則所有元素之間都沒有任何字符
返回值:
一個所有數(shù)組元素連接的字符串
例子:
['h','e','l','l','o'].join(); // "h,e,l,l,o"
['h','e','l','l','o'].join(""); // "hello"
----------------------------------------------------------------------------
----------------------------------------------------------------------------
Array.prototype.toString()
描述:
返回一個由所有數(shù)組元素組合而成的字符串,默認以','進行分割
遮蔽了原型鏈上的 Object.prototype.toString() 方法
無參:
返回值:
返回一個字符串搀玖,表示指定的數(shù)組及其元素
例子:
const names=['panda','cat','dog','mokey'];
names.toString(); // "panda,cat,dog,mokey"
----------------------------------------------------------------------------
----------------------------------------------------------------------------
Array.prototype.toLocaleString()
描述:
返回一個由所有數(shù)組元素組合而成的本地化后的字符串余境。
數(shù)組中的元素將會使用各自的 toLocaleString 方法:
Object: Object.prototype.toLocaleString()
Number: Number.prototype.toLocaleString()
Date: Date.prototype.toLocaleString()
無參:
返回值:
返回一個由所有數(shù)組元素組合而成的本地化后的字符串
----------------------------------------------------------------------------
遍歷方法
Array.prototype.forEach
Array.prototype.forEach(callback[, thisArg]):
描述:
為數(shù)組中的每個元素執(zhí)行一次回調(diào)函數(shù)
參數(shù):
callback:
為數(shù)組中每個元素執(zhí)行的函數(shù),該函數(shù)接收三個參數(shù):
currentValue(當(dāng)前值):
數(shù)組中正在處理的當(dāng)前元素灌诅。
index(索引):
數(shù)組中正在處理的當(dāng)前元素的索引芳来。
array:
forEach()方法正在操作的數(shù)組。
thisArg可選
當(dāng)執(zhí)行回調(diào) 函數(shù)時用作this的值(參考對象)
返回值:
undefined
例子:
const arr=[1,2,3,4,5];
const newArr=[];
arr.forEach((item,index)=>{
if(index%2===0){
item+=item;
newArr.push(item);
}
if(index%2!==0){
item*=item;
newArr.push(item)
}
});
console.log(newArr); // [2, 4, 6, 16, 10]
// 利用類創(chuàng)建對象,而且forEach中參數(shù)以箭頭函數(shù)方式傳入
class Counter {
constructor(){
this.sum=0;
this.count=0;
}
add(array){
array.forEach(item=>{
this.sum+=item;
++this.count
})
}
}
const obj=new Counter();
obj.add([1,2,3,4,5]);
obj.sum; // 15
obj.count; // 5
// 利用類創(chuàng)建對象,而且forEach中參數(shù)以函數(shù)方式傳入
class Counter {
constructor(){
this.sum=0;
this.count=0;
}
add(array){
array.forEach(function(item){
this.sum+=item;
++this.count
})
}
}
const obj=new Counter();
obj.add([1,2,3,4,5]);
obj.sum; // Uncaught TypeError: Cannot read property 'sum' of undefined
// 利用類創(chuàng)建對象,而且forEach中參數(shù)以箭頭函數(shù)方式傳入延塑,并傳入this
class Counter {
constructor(){
this.sum=0;
this.count=0;
}
add(array){
array.forEach(function(item){
this.sum+=item;
++this.count
},this)
}
}
const obj=new Counter();
obj.add([1,2,3,4,5]);
obj.sum; // 15
Array.prototype.every
Array.prototype.every(callback[, thisArg])
描述:
測試數(shù)組的所有元素是否都通過了指定函數(shù)的測試
如果數(shù)組中的每個元素都滿足測試函數(shù)绣张,則返回 true,否則返回 false
參數(shù):
callback:
為數(shù)組中每個元素執(zhí)行的函數(shù)关带,該函數(shù)接收三個參數(shù):
currentValue(當(dāng)前值):
數(shù)組中正在處理的當(dāng)前元素侥涵。
index(索引):
數(shù)組中正在處理的當(dāng)前元素的索引。
array:
forEach()方法正在操作的數(shù)組宋雏。
thisArg可選
當(dāng)執(zhí)行回調(diào) 函數(shù)時用作this的值(參考對象)
返回值:
boolean值
例子:
const compare=(item,index)=>{
return item>2;
};
[1,2,3,4,5].every(compare); // false
[3,4,5,6,7].every(compare); // true
Array.prototype.some
Array.prototype.some(callback[, thisArg])
描述:
如果數(shù)組中至少有一個元素滿足測試函數(shù)芜飘,則返回 true,否則返回 false
參數(shù):
callback:
為數(shù)組中每個元素執(zhí)行的函數(shù)磨总,該函數(shù)接收三個參數(shù):
currentValue(當(dāng)前值):
數(shù)組中正在處理的當(dāng)前元素嗦明。
index(索引):
數(shù)組中正在處理的當(dāng)前元素的索引。
array:
forEach()方法正在操作的數(shù)組蚪燕。
thisArg可選
當(dāng)執(zhí)行回調(diào) 函數(shù)時用作this的值(參考對象)
返回值:
boolean值
例子:
const compare=(item,index)=>{
return item>4;
};
[1,2,3,4,0].some(compare); // false
[3,4,5,6,7].some(compare); // true
Array.prototype.filter
Array.prototype.filter(callback[, thisArg])
描述:
將所有在過濾函數(shù)中返回 true 的數(shù)組元素放進一個新數(shù)組中并返回
參數(shù):
callback:
為數(shù)組中每個元素執(zhí)行的函數(shù)娶牌,該函數(shù)接收三個參數(shù):
currentValue(當(dāng)前值):
數(shù)組中正在處理的當(dāng)前元素。
index(索引):
數(shù)組中正在處理的當(dāng)前元素的索引馆纳。
array:
forEach()方法正在操作的數(shù)組诗良。
thisArg可選
當(dāng)執(zhí)行回調(diào) 函數(shù)時用作this的值(參考對象)
返回值:
一個新的通過測試的元素的集合的數(shù)組
例子:
const compare=(item,index)=>{
return item>2;
};
[1,2,3,4,0].filter(compare); // [3,4]
Array.prototype.map
Array.prototype.map(callback[, thisArg])
描述:
返回一個由回調(diào)函數(shù)的返回值組成的新數(shù)組
參數(shù):
callback:
為數(shù)組中每個元素執(zhí)行的函數(shù),該函數(shù)接收三個參數(shù):
currentValue(當(dāng)前值):
數(shù)組中正在處理的當(dāng)前元素鲁驶。
index(索引):
數(shù)組中正在處理的當(dāng)前元素的索引鉴裹。
array:
forEach()方法正在操作的數(shù)組。
thisArg可選
當(dāng)執(zhí)行回調(diào) 函數(shù)時用作this的值(參考對象)
返回值:
一個新數(shù)組钥弯,每個元素都是回調(diào)函數(shù)的結(jié)果
例子:
['1', '2', '3'].map( str => parseInt(str) ); // [1,2,3]
Array.prototype.keys
Array.prototype.keys()
描述:
返回一個數(shù)組迭代器對象径荔,該迭代器會包含所有數(shù)組元素的鍵
無參:
返回值:
一個新的 Array 迭代器對象
例子:
const arr = ["a", "b", "c"];
const iterator = arr.keys();
console.log(iterator.next()); // { value: 0, done: false }
console.log(iterator.next()); // { value: 1, done: false }
console.log(iterator.next()); // { value: 2, done: false }
console.log(iterator.next()); // { value: undefined, done: true}
Array.prototype.values
Array.prototype.values()
描述:
返回一個數(shù)組迭代器對象,該迭代器會包含所有數(shù)組元素的值
谷歌脆霎,火狐暫未實現(xiàn)
Array.prototype.entries
Array.prototype.entries()
描述:
返回一個數(shù)組迭代器對象总处,該迭代器會包含所有數(shù)組元素的鍵值對
無參:
返回值:
一個新的 Array 迭代器對象
例子:
const arr = ["a", "b", "c"];
const iterator = arr.entries();
console.log(iterator.next()); // { value:[0,'a'], done:false}
console.log(iterator.next()); // { value:[1,'b'], done:false}
console.log(iterator.next()); // { value:[2,'c'], done:false}
console.log(iterator.next()); // { value:undefined, done:done}
Array.prototype.reduce
Array.prototype.reduce(callback[, initialValue]):
描述:
從左到右為每個數(shù)組元素執(zhí)行一次回調(diào)函數(shù),并把上次回調(diào)函數(shù)的返回值放在一個暫存器中傳給下次回調(diào)函數(shù)绪穆,
并返回最后一次回調(diào)函數(shù)的返回值
參數(shù):
callback
執(zhí)行數(shù)組中每個值的函數(shù)辨泳,包含四個參數(shù):
accumulator
累加器累加回調(diào)的返回值; 它是上一次調(diào)用回調(diào)時返回的累積值虱岂,或initialValue
currentValue
數(shù)組中正在處理的元素。
currentIndex
數(shù)組中正在處理的當(dāng)前元素的索引菠红。 如果提供了initialValue第岖,則索引號為0,否則為索引為1试溯。
array
調(diào)用reduce的數(shù)組
返回值:
強制返回accumulator,否則為undefined
initialValue
用作第一個調(diào)用 callback的第一個參數(shù)的值蔑滓。 如果沒有提供初始值,則將使用數(shù)組中的第一個元素
返回值:
函數(shù)累計處理的結(jié)果
例子:
let arr=[];
const add=(init,item,index)=>{
if(item%2===0){
init+=item;
arr.push(index);
return init;
}else{
return init
}
};
let sum=[1,2,3,4,5,6].reduce(add);
console.log(sum); // 13
console.log(arr); // [1, 3, 5]
[7,8,9,10].reduce(add,sum); // 31
Array.prototype.reduceRight
Array.prototype.reduceRight(callback[, initialValue])
描述:
從右到左為每個數(shù)組元素執(zhí)行一次回調(diào)函數(shù)遇绞,并把上次回調(diào)函數(shù)的返回值放在一個暫存器中傳給下次回調(diào)函數(shù)键袱,
并返回最后一次回調(diào)函數(shù)的返回值
參數(shù):
callback
執(zhí)行數(shù)組中每個值的函數(shù),包含四個參數(shù):
accumulator
累加器累加回調(diào)的返回值; 它是上一次調(diào)用回調(diào)時返回的累積值摹闽,或initialValue
currentValue
數(shù)組中正在處理的元素蹄咖。
currentIndex
數(shù)組中正在處理的當(dāng)前元素的索引。 如果提供了initialValue付鹿,則索引號為0澜汤,否則為索引為1。
array
調(diào)用reduce的數(shù)組
返回值:
強制返回accumulator,否則為undefined
initialValue
用作第一個調(diào)用 callback的第一個參數(shù)的值舵匾。 如果沒有提供初始值俊抵,則將使用數(shù)組中的第一個元素
返回值:
函數(shù)累計處理的結(jié)果
例子:
let arr=[];
const add=(init,item,index)=>{
if(item%2===0){
init+=item;
arr.push(index);
return init;
}else{
return init
}
};
let sum=[1,2,3,4,5,6].reduceRight(add);
console.log(sum); // 12
console.log(arr); // [3,1]
[7,8,9,10].reduceRight(add,sum); // 31
對方法一些特性進行總結(jié):
以下方法會改變調(diào)用他們自身的對象的值:
Array.prototype.copyWithin()
Array.prototype.fill()
Array.prototype.pop()
Array.prototype.push()
Array.prototype.reverse()
Array.prototype.shift()
Array.prototype.sort()
Array.prototype.splice()
Array.prototype.unshift()
以下方法絕對不會改變調(diào)用它們的對象的值,只會返回一個新的數(shù)組或者返回一個其它的期望值:
Array.prototype.concat()
Array.prototype.includes()
Array.prototype.join()
Array.prototype.slice()
Array.prototype.toString()
Array.prototype.toLocaleString()
Array.prototype.indexOf()
Array.prototype.lastIndexOf()
與字符串共有的方法
Array.prototype.includes()
Array.prototype.slice()
Array.prototype.concat()
Array.prototype.toString()
關(guān)于數(shù)組遍歷方法返回值總結(jié):
Array.prototype.forEach(callback[, thisArg])
無返回值
Array.prototype.every(callback[, thisArg])
返回boolean值
Array.prototype.some(callback[, thisArg])
返回boolean值
Array.prototype.filter(callback[, thisArg])
返回 true 的數(shù)組元素組成的新數(shù)組
Array.prototype.map(callback[, thisArg])
返回一個由回調(diào)函數(shù)的返回值組成的新數(shù)組坐梯。
Array.prototype.keys()
返回一個數(shù)組迭代器對象徽诲,該迭代器會包含所有數(shù)組元素的鍵。
Array.prototype.values()
返回一個數(shù)組迭代器對象吵血,該迭代器會包含所有數(shù)組元素的值
Array.prototype.entries()
返回一個數(shù)組迭代器對象谎替,該迭代器會包含所有數(shù)組元素的鍵值對。
Array.prototype.reduce()
Array.prototype.reduceRight()
Array.prototype[@@iterator]()