var arr1 = new Array(); // 建立一個空數組
var arr2 = new Array(10); // 建立一個包含20項的數組
var arr3 = new Array('liu', 'wang', 'li'); // 建立一個包含3個字符串的數組
複製代碼
var arr1 = []; // 建立一個空數組
var arr2 = [10]; // 建立一個包含1項的數組
var arr3 = ['liu', 'wang', 'li']; // 建立一個包含3個字符串的數組
複製代碼
方法名 | 描述 |
---|---|
join | 把數組的全部元素放入一個字符串,元素經過指定的分隔符進行分隔 |
pop | 刪除並返回數組的最後一個元素 |
push | 向數組的末尾添加一個或更多元素,並返回新的長度 |
shift | 刪除並返回數組的第一個元素 |
unshift | 向數組的開頭添加一個或更多元素,並返回新的長度 |
slice | 從某個已有的數組返回指定的元素 |
indexOf | 返回第一個與給定參數相等的數組元素的索引,沒有找到則返回-1 |
lastIndexOf | 返回在數組中搜索到的與給定參數相等的元素的索引裏的最大的值,沒有找到則返回-1 |
sort | 對數組的元素進行排序 |
splice | 刪除元素,並向數組添加新元素 |
toString | 把數組轉換爲字符串,並返回結果 |
toLocaleString | 把數組轉換爲本地字符串,並返回結果 |
valueOf | 返回數組對象的原始值 |
forEach | 對數組中的每一項運行指定函數,這個方法沒有返回值 |
concat | 鏈接2個或更多數組,並返回結果 |
every | 對數組中的每一項運行指定函數,若是該函數對每一項都返回true,則返回true |
some | 對數組中的每一項運行指定函數,若是任一項返回true,則返回true |
filter | 對數組中的每一項運行指定函數,返回該函數會返回true的項組成的數組 |
reverse | 顛倒數組中元素的順序 |
map | 對數組中的每一項運行指定函數,返回每次函數調用的結果組成的數組 |
reduce | 接收一個函數做爲累加器,數組中的每一個值(從左到右)開始縮減,最終計算爲一個值 |
reduceRight | 接收一個函數做爲累加器,數組中的每一個值(從右到左)開始縮減,最終計算爲一個值 |
PS:原始值是指固定而簡單的值,存放在棧中的簡單數據段,它們的值直接存儲在變量訪問的位置。git
JavaScript中有五種原始類型,也叫基本類型:
Number、String、Boolean、Undefined、Nullgithub
定義和用法
數組
join()方法用於把數組中的全部元素放入一個字符串。
元素是經過指定的分隔符進行分隔的。bash
語法
微信
arrayObject.join(separator)函數
參數 | 描述 |
---|---|
seperator | 可選。指定要使用的分隔符,若是省略該參數,則使用逗號做爲分隔符 |
返回值
ui
返回一個字符串。該字符串是經過把 arrayObject 的每一個元素轉換爲字符串,而後把這些字符串鏈接起來,在兩個元素之間插入 separator 字符串而生成的。
this
var arr = new Array(3);
arr[0] = "Geroge";
arr[1] = "John";
arr[2] = "Thomas";
var str1 = arr.join();
var str2 = arr.join('');
var str3 = arr.join(' ');
var str4 = arr.join('-');
console.log(str1); // "Geroge,John,Thomas"
console.log(str2); // "GerogeJohnThomas"
console.log(str3); // "Geroge John Thomas"
console.log(str4); // "Geroge-John-Thomas"
複製代碼
定義和用法
編碼
pop()方法用於刪除並返回數組的最後一個元素。spa
語法
arrayObject.pop()
返回值
arrayObject 的最後一個元素。
說明
pop() 方法將刪除 arrayObject 的最後一個元素,把數組長度減 1,而且返回它刪除的元素的值。若是數組已經爲空,則 pop() 不改變數組,並返回 undefined 值。
var arr = new Array(3);
arr[0] = "Geroge";
arr[1] = "John";
arr[2] = "Thomas";
console.log(arr); // ["Geroge", "John", "Thomas"]
console.log(arr.pop()); // "Thomas"
console.log(arr); // ["Geroge", "Thomas"]
複製代碼
定義和用法
push()方法可向數組的末尾添加一個或多個元素,並返回新的長度。
語法
arrayObject.push(newElement1, newElement2, ..., newElementX)
參數 | 描述 |
---|---|
newElement1 | 必需。要添加到數組末尾的第一個元素 |
newElement2 | 可選。要添加到數組末尾的第二個元素 |
newElementX | 可選。可添加多個元素 |
返回值
把指定的值添加到數組後的新長度。
說明
push() 方法可把它的參數順序添加到 arrayObject 的尾部。它直接修改 arrayObject,而不是建立一個新的數組。push() 方法和 pop() 方法使用數組提供的先進後出棧的功能。
var arr = new Array(3);
arr[0] = "Geroge";
arr[1] = "John";
arr[2] = "Thomas";
console.log(arr); // ["Geroge", "John", "Thomas"]
console.log(arr.push("James")); // 4
console.log(arr); // ["Geroge", "John", "Thomas", "James"]
console.log(arr.push("Peter", "Sara")); // 6
console.log(arr); // ["Geroge", "John", "Thomas", "James", "Peter", "Sara"]
複製代碼
定義和用法
shift()方法用於把數組的第一個元素從其中刪除,並返回第一個元素的值。
語法
arrayObject.shift()
返回值
數組原來的第一個元素的值。
說明
若是數組是空的,那麼 shift() 方法將不進行任何操做,返回 undefined 值。請注意,該方法不建立新數組,而是直接修改原有的 arrayObject。
var arr = new Array(3);
arr[0] = "Geroge";
arr[1] = "John";
arr[2] = "Thomas";
console.log(arr); // ["Geroge", "John", "Thomas"]
console.log(arr.shift()); // "Geroge"
console.log(arr); // ["John", "Thomas"]
複製代碼
定義和用法
unshift()方法可向數組的開頭添加一個或多個元素,並返回新的長度。
語法
arrayObject.unshift(newElement1, newElement2, ..., newElementX)
參數 | 描述 |
---|---|
newElement1 | 必需。要添加到數組開頭的第一個元素 |
newElement2 | 可選。要添加到數組開頭的第二個元素 |
newElementX | 可選。可添加多個元素 |
返回值
arrayObject 的新長度。
說明
unshift() 方法將把它的參數插入 arrayObject 的頭部,並將已經存在的元素順次地移到較高的下標處,以便留出空間。該方法的第一個參數將成爲數組的新元素 0,若是還有第二個參數,它將成爲新的元素 1,以此類推。
請注意,unshift() 方法不建立新的建立,而是直接修改原有的數組。
var arr = new Array(3);
arr[0] = "Geroge";
arr[1] = "John";
arr[2] = "Thomas";
console.log(arr); // ["Geroge", "John", "Thomas"]
console.log(arr.unshift("James")); // 4
console.log(arr); // ["James", "Geroge", "John", "Thomas"]
console.log(arr.unshift("Peter", "Sara")); // 6
console.log(arr); // ["Peter", "Sara", "James", "Geroge", "John", "Thomas"]
複製代碼
定義和用法
slice()方法可從已有的數組中返回選定的元素。slice()方法不改變原數組。
語法
arrayObject.slice(start, end)
參數 | 描述 |
---|---|
start | 必需。規定從何處開始選取。 若是是負數,那麼它規定從數組尾部開始算起的位置。 也就是說,-1指最後一個元素,-2指倒數第二個元素,以此類推。 |
end | 可選。規定從何處結束選取。 該參數是數組片段結束處的數組下標。 若是沒有指定該參數,那麼切分的數組包含從start到數組結束的全部元素。 若是這個參數是負數,那麼它規定的是從數組尾部開始算起的元素。 |
返回值
返回一個新的數組,包含從 start 到 end (不包括該元素)的 arrayObject 中的元素。
說明
請注意,該方法並不會修改數組,而是返回一個子數組。若是想刪除數組中的一段元素,應該使用方法 Array.splice()。
var arr = new Array(6);
arr[0] = "George";
arr[1] = "John";
arr[2] = "Thomas";
arr[3] = "James";
arr[4] = "Adrew";
arr[5] = "Martin";
console.log(arr); // ["Geroge", "John", "Thomas", "James", "Adrew", "Martin"]
console.log(arr.slice(0)); // ["Geroge", "John", "Thomas", "James", "Adrew", "Martin"]
console.log(arr.slice(1)); // ["John", "Thomas", "James", "Adrew", "Martin"]
console.log(arr.slice(1, 3)); // ["John", "Thomas"]
console.log(arr.slice(1, -2)); // ["John", "Thomas", "James"]
console.log(arr.slice(-1, -2)); // []
console.log(arr.slice(-2, -1)); // ["Adrew"]
console.log(arr); // ["Geroge", "John", "Thomas", "James", "Adrew", "Martin"]
複製代碼
定義和用法
indexOf()方法可返回某個指定的值在數組中首次出現的位置。從左往右找,找不到返回-1。
語法
arrayObject.indexOf(searchValue, fromIndex)
參數 | 描述 |
---|---|
searchValue | 必需。規定需檢索的值。 |
fromIndex | 可選的整數參數,開始查找的位置。 若是該索引值大於或等於數組長度,意味着不會在數組裏查找,返回-1。 若是參數中提供的索引值是一個負值,則將其做爲數組末尾的一個抵消, 即-1表示從最後一個元素開始查找,-2表示從倒數第二個元素開始查找 ,以此類推。 注意:若是參數中提供的索引值是一個負值,並不改變其查找順序, 查找順序仍然是從前向後查詢數組。若是抵消後的索引值仍小於0, 則整個數組都將會被查詢。其默認值爲0 |
var arr = new Array(6);
arr[0] = "George";
arr[1] = "John";
arr[2] = "Thomas";
arr[3] = "James";
arr[4] = "Adrew";
arr[5] = "Martin";
console.log(arr.indexOf('Thomas')); // 2
console.log(arr.indexOf('Thomas', 2)); // 2
console.log(arr.indexOf('Thomas', 3)); // -1
console.log(arr.indexOf('Thomas', -4)); // 2
console.log(arr.indexOf('Thomas', -3)); // -1
console.log(arr.indexOf('Peter')); // -1
複製代碼
定義和用法
lastIndexOf()方法可返回某個指定的值在數組中首次出現的位置。從右往左找,找不到返回-1。
語法
arrayObject.indexOf(searchValue, fromIndex)
參數 | 描述 |
---|---|
searchValue | 必需。規定需檢索的值。 |
fromIndex | 可選的整數參數,今後位置開始逆向查找。 默認爲數組的長度減 1,即整個數組都被查找。 若是該值大於或等於數組的長度,則整個數組會被查找。 若是爲負值,將其視爲從數組末尾向前的偏移。 即便該值爲負,數組仍然會被從後向前查找。 若是該值爲負時,其絕對值大於數組長度,則方法返回 -1,即數組不會被查找 |
var arr = new Array(6);
arr[0] = "George";
arr[1] = "John";
arr[2] = "Thomas";
arr[3] = "James";
arr[4] = "Adrew";
arr[5] = "Martin";
console.log(arr.lastIndexOf('Thomas')); // 2
console.log(arr.lastIndexOf('Thomas', 2)); // 2
console.log(arr.lastIndexOf('Thomas', 3)); // 2
console.log(arr.lastIndexOf('Thomas', 1)); // -1
console.log(arr.lastIndexOf('Thomas', -4)); // 2
console.log(arr.lastIndexOf('Thomas', -3)); // 2
console.log(arr.lastIndexOf('Peter')); // -1
複製代碼
定義和用法
sort()方法用於對數組的元素進行排序。
語法
arrayObject.sort(sortby)
參數 | 描述 |
---|---|
sortby | 可選。規定排序順序。必須是函數。 |
返回值
對數組的引用。請注意,數組在原數組上進行排序,不生成副本。
說明
若是調用該方法時沒有使用參數,將按字母順序對數組中的元素進行排序,說的更精確點,是按照字符編碼的順序進行排序。要實現這一點,首先應把數組的元素都轉換成字符串(若有必要),以方便比較。
若是想按照其餘標準進行排序,就須要提供比較函數,該函數要比較兩個值,而後返回一個用於說明這兩個值的相對順序的數字。比較函數應該具備兩個參數a和b,其返回值以下:
即順序 return a - b; 倒序 return b - a;
a在b前返回負數,a在b後返回正數
var arr = new Array(6);
arr[0] = "10";
arr[1] = "5";
arr[2] = "40";
arr[3] = "25";
arr[4] = "1000";
arr[5] = "1";
console.log(arr); // ["10", "5", "40", "25", "1000", "1"]
console.log(arr.sort()); // ["1", "10", "1000", "25", "40", "5"]
console.log(arr); // ["1", "10", "1000", "25", "40", "5"]
複製代碼
var arr = new Array(6);
arr[0] = "10";
arr[1] = "5";
arr[2] = "40";
arr[3] = "25";
arr[4] = "1000";
arr[5] = "1";
function orderNumber (a, b) {
return a - b;
}
function descOrderNumber (a, b) {
return b - a;
}
console.log(arr); // ["10", "5", "40", "25", "1000", "1"]
console.log(arr.sort(orderNumber)); // ["1", "5", "10", "25", "40", "1000"]
console.log(arr.sort(descOrderNumber)); // ["1000", "40", "25", "10", "5", "1"]
console.log(arr); // ["1000", "40", "25", "10", "5", "1"]
複製代碼
定義和用法
splice()方法向/從數組中添加/刪除項目,而後返回被刪除的項目。該方法會改變原始數組。
語法
arrayObject.splice(index, howmany, item1, ......, itemX)
參數 | 描述 |
---|---|
index | 必需。整數,規定添加/刪除項目的位置,使用負數可從數組結尾處規定位置。 |
howmany | 必需。要刪除的項目數量。若是設置爲0,則不會刪除項目。 |
item1,......,itemX | 可選。向數組添加的新項目。 |
返回值
類型 | 描述 |
---|---|
Array | 包含被刪除項目的新數組,若是有的話。 |
說明
splice()方法可刪除從index處開始的0個或多個元素,而且用參數列表中聲明的一個或多個值來替換那些被刪除的元素。若是從arrayObject中刪除了元素,則返回的是含有被刪除的元素的數組。
var arr = new Array(6);
arr[0] = "George";
arr[1] = "John";
arr[2] = "Thomas";
arr[3] = "James";
arr[4] = "Adrew";
arr[5] = "Martin";
console.log(arr); // ["Geroge", "John", "Thomas", "James", "Adrew", "Martin"]
console.log(arr.splice(1, 1, 'Peter', 'Sara')); // ["John"]
console.log(arr); // ["Geroge", "Peter", "Sara", "Thomas", "James", "Adrew", "Martin"]
console.log(arr.splice(1, 0, 'Ella')); // []
console.log(arr); // ["Geroge", "Ella", "Peter", "Sara", "Thomas", "James", "Adrew", "Martin"]
複製代碼
定義和用法
toString()方法可把數組轉換爲字符串,並返回結果。
語法
arrayObject.toString()
返回值
arrayObject的字符串表示。返回值與沒有參數的join()方法返回的字符串相同。
說明
當數組用於字符串環境時,JavaScript會調用這一方法將數組自動轉換成字符串。可是在某些狀況下,須要顯式地調用該方法。
var arr = new Array(4);
arr[0] = "Geroge";
arr[1] = "John";
arr[2] = "Thomas";
arr[3] = 20;
console.log(arr.toString()); // "Geroge,John,Thomas,20"
複製代碼
定義和用法
toLocaleString()方法可把數組轉換爲本地字符串,並返回結果。
語法
arrayObject.toLocaleString()
返回值
arrayObject的本地字符串表示。
說明
首先調用每一個數組元素的 toLocaleString() 方法,而後使用地區特定的分隔符把生成的字符串鏈接起來,造成一個字符串。
var arr = new Array(4);
arr[0] = "Geroge";
arr[1] = "John";
arr[2] = "Thomas";
arr[3] = 20;
console.log(arr.toLocaleString()); // "Geroge,John,Thomas,20"
複製代碼
定義和用法
valueOf()方法返回Array對象的原始值。該原始值由Array對象派生的全部對象繼承。valueOf()方法一般由JavaScript在後臺自動調用,並不顯式地出如今代碼中。
語法
arrayObject.valueOf()
var arr = new Array(4);
arr[0] = "Geroge";
arr[1] = "John";
arr[2] = "Thomas";
arr[3] = 20;
console.log(arr.valueOf()); // ["Geroge", "John", "Thomas", 20]
複製代碼
定義和用法
forEach()方法用於調用數組的每一個元素,並將元素傳遞給回調函數。forEach()對於空數組是不會執行回調函數的。
語法
arrayObject.forEach(function (value, index, arr) {}, thisValue)
參數 | 描述 |
---|---|
function(currentValue, index, arr) | 必需。數組中每一個元素須要調用的函數。 currentValue: 必需。當前元素。 index: 可選。當前元素的索引值。 arr: 可選。當前元素所屬的數組對象。 |
thisValue | 可選。傳遞給函數的值通常用'this'值。 若是這個參數爲空,嚴格模式下把'undefined'會傳遞給'this'值,普通模式下傳入'window'。 |
var arr = new Array(3);
arr[0] = "Geroge";
arr[1] = "John";
arr[2] = "Thomas";
arr.forEach(function (value, index, arr) {
console.log(value); // "Geroge" "John" "Thomas"
console.log(index); // 0 1 2
console.log(arr); // ["Geroge", "John", "Thomas"]
console.log(this); // window
});
arr.forEach(function (value, index, arr) {
console.log(value); // "Geroge" "John" "Thomas"
console.log(index); // 0 1 2
console.log(arr); // ["Geroge", "John", "Thomas"]
console.log(this); // ["Geroge", "John", "Thomas"]
}, arr);
複製代碼
定義和用法
concat()方法用於鏈接兩個或多個數組。該方法不會改變現有的數組,而僅僅會返回被鏈接數組的一個副本。
語法
arrayObject.concat(arrayX,arrayX,......,arrayX)
參數 | 描述 |
---|---|
arrayX | 必需。該參數能夠是具體的值,也能夠是數組對象。能夠是任意多個。 |
返回值
返回一個新的數組。該數組是經過把全部arrayX參數添加到arrayObject中生成的。若是要進行concat()操做的參數是數組,那麼添加的是數組中的元素,而不是數組。
var a = [1, 2, 3];
console.log(a.concat(4, 5, [6, 7], 8, 9)); // [1, 2, 3, 4, 5, 6, 7, 8, 9]
複製代碼
定義和用法
every()方法用於檢測數組全部元素是否都符合指定條件(經過函數提供)。 every()方法使用指定函數檢測數組中的全部元素:
注意:every()不會對空數組進行檢測。 注意:every()不會改變原始數組。
語法
arrayObject.every(function (currentValue, index, arr) {}, thisValue)
參數 | 描述 |
---|---|
function (currentValue, index, arr) | 必需。函數,數組中的每一個元素都會執行這個函數。 currentValue: 必需。當前元素。 index: 可選。當前元素的索引值。 arr: 可選。當前元素所屬的數組對象。 |
thisValue | 可選。對象做爲該執行回調時使用,傳遞給函數。 |
說明
有一個返回false,則整個every()返回值爲false,而且不會執行後續其餘項的回調函數。 空數組的every()直接返回true。
var ages = [10, 20, 24, 32, 40];
var result = ages.every(function (value, index, arr) {
return value > 25;
});
console.log(result); // false
ages = [];
result = ages.every(function (value, index, arr) {
return value > 25;
});
console.log(result); // true
複製代碼
定義和用法
some()方法用於檢測數組全部元素是否知足指定條件(經過函數提供)。 every()方法會依次執行數組的每一個元素:
注意:some()不會對空數組進行檢測。 注意:some()不會改變原始數組。
語法
arrayObject.some(function (currentValue, index, arr) {}, thisValue)
參數 | 描述 |
---|---|
function (currentValue, index, arr) | 必需。函數,數組中的每一個元素都會執行這個函數。 currentValue: 必需。當前元素。 index: 可選。當前元素的索引值。 arr: 可選。當前元素所屬的數組對象。 |
thisValue | 可選。對象做爲該執行回調時使用,傳遞給函數。 |
說明
有一個返回true,則整個some()返回值爲true,而且不會執行後續其餘項的回調函數。 空數組的some()直接返回false。
var ages = [10, 20, 24, 32, 40];
var result = ages.some(function (value, index, arr) {
return value > 25;
});
console.log(result); // true
ages = [];
result = ages.some(function (value, index, arr) {
return value > 25;
});
console.log(result); // false
複製代碼
定義和用法
filter()方法建立一個新的數組,新數組中的元素是經過檢查指定數組中符合條件的全部元素。
注意:filter()不會對空數組進行檢測。 注意:filter()不會改變原始數組。
語法
arrayObject.filter(function (currentValue, index, arr) {}, thisValue)
參數 | 描述 |
---|---|
function (currentValue, index, arr) | 必需。函數,數組中的每一個元素都會執行這個函數。 currentValue: 必需。當前元素。 index: 可選。當前元素的索引值。 arr: 可選。當前元素所屬的數組對象。 |
thisValue | 可選。對象做爲該執行回調時使用,傳遞給函數。 |
說明
將全部返回true的數組項取出來組成一個新的數組。
var ages = [10, 20, 24, 32, 40];
var result = ages.filter(function (value, index, arr) {
return value > 25;
});
console.log(result); // [32, 40]
console.log(ages); // [10, 20, 24, 32, 40]
ages = [];
result = ages.filter(function (value, index, arr) {
return value > 25;
});
console.log(result); // []
複製代碼
定義和用法
reverse()方法用於顛倒數組中元素的順序。會改變原數組。
語法
arrayObject.reverse()
var arr = new Array(3);
arr[0] = "George";
arr[1] = "John";
arr[2] = "Thomas";
console.log(arr); // ["Geroge", "John", "Thomas"]
console.log(arr.reverse()); // ["Thomas", "John", "Geroge"]
console.log(arr); // ["Thomas", "John", "Geroge"]
複製代碼
定義和用法
map()方法返回一個新數組,數組中的元素爲原始數組元素調用函數處理後的值。map()方法按照原始數組元素順序依次處理元素。
注意:map()不會對空數組進行檢測。 注意:map()不會改變原始數組。
語法
arrayObject.map(function (currentValue, index, arr) {}, thisValue)
參數 | 描述 |
---|---|
function (currentValue, index, arr) | 必需。函數,數組中的每一個元素都會執行這個函數。 currentValue: 必需。當前元素。 index: 可選。當前元素的索引值。 arr: 可選。當前元素所屬的數組對象。 |
thisValue | 可選。對象做爲該執行回調時使用,傳遞給函數。 |
var numbers = [65, 20, 11, 5];
var arr = numbers.map(function (value, index, arr) {
return value * 2;
})
console.log(numbers); // [65, 20, 11, 5]
console.log(arr); // [130, 40, 22, 10]
複製代碼
定義和用法
reduce()方法接收一個函數做爲累加器,數組中的每一個值(從左到右)開始縮減,最終計算爲一個值。
注意:reduce()對於空數組是不會執行回調函數的。
語法
arrayObject.reduce(function (total, currentValue, currentIndex, arr) {}, initialValue)
參數 | 描述 |
---|---|
function (total, currentValue, currentIndex, arr) | 必需。函數,數組中的每一個元素都會執行這個函數。 total: 必需。初始值,或者計算結束後的返回值。 currentValue: 必需。當前元素。 currentIndex: 可選。當前元素的索引。 arr: 可選。當前元素所屬的數組對象。 |
initialValue | 可選。傳遞給函數的初始值。 |
var numbers = [15, 2, 1, 7];
var total = numbers.reduce(function (total, currentValue) {
console.log(total); // 15 17 18 25
console.log(currentValue); // 2 1 7
return total + currentValue;
});
console.log(total); // 25
console.log(numbers); // [15, 2, 1, 7]
total = numbers.reduce(function (total, currentValue) {
console.log(total); // 20 35 37 38 45
console.log(currentValue); // 15 2 1 7
return total + currentValue;
}, 20);
console.log(total); // 45
console.log(numbers); // [15, 2, 1, 7]
複製代碼
定義和用法
reduceRight()方法的功能和reduce()功能是同樣的,不一樣的是reduceRight()從數組的末尾向前將數組中的數組項作累加。
注意:reduceRight()對於空數組是不會執行回調函數的。
語法
arrayObject.reduceRight(function (total, currentValue, currentIndex, arr) {}, initialValue)
參數 | 描述 |
---|---|
function (total, currentValue, currentIndex, arr) | 必需。函數,數組中的每一個元素都會執行這個函數。 total: 必需。初始值,或者計算結束後的返回值。 currentValue: 必需。當前元素。 currentIndex: 可選。當前元素的索引。 arr: 可選。當前元素所屬的數組對象。 |
initialValue | 可選。傳遞給函數的初始值。 |
var numbers = [15, 2, 1, 7];
var total = numbers.reduceRight(function (total, currentValue) {
console.log(total); // 7 8 10 25
console.log(currentValue); // 1 2 15
return total + currentValue;
});
console.log(total); // 25
console.log(numbers); // [15, 2, 1, 7]
total = numbers.reduceRight(function (total, currentValue) {
console.log(total); // 20 27 28 30 45
console.log(currentValue); // 7 1 2 15
return total + currentValue;
}, 20);
console.log(total); // 45
console.log(numbers); // [15, 2, 1, 7]
複製代碼
本文會同步到個人我的博客,完整代碼能夠到個人github倉庫查看,若是對你有幫助的話歡迎點一個Star~~