JavaScript中數組的函數有哪些?相信很多沒有經驗的人對此束手無策,為此本文總結了問題出現的原因和解決方法,通過這篇文章希望你能解決這個問題。
callbackFn 回調函數
thisArg 執行 callbackFn 時使用的 this 值
currentValue 數組中正在處理的元素
index 當前索引
array 源數組
accumulator 累加器
initialValue reduce reduceRight 第一次調用 callbackFn 函數時的第一個參數的值默認值
element 自己實現的 this 對象
語法: arr.forEach(callbackFn(currentValue [, index [, array]])[, thisArg])
方法功能: 對數組的每個元素執行一次給定的函數。
返回:undefined。
自定義函數:myForEach。
Array.prototype.myForEach = function(callbackFn, thisArg) {
if (typeof callbackFn !== 'function') throw ('callbackFn參數必須是函數');
let element = this,
len = element && element.length || 0;
if (!thisArg) thisArg = element;
for (let index = 0; index < len; index++) {
callbackFn.call(thisArg, element[index], index, element);
}
};語法: var newArray = arr.filter(callbackFn(currentValue[, index[, array]])[, thisArg])
方法功能: 創建一個新數組, 其包含通過所提供函數實現的測試的所有元素。
返回:一個新的、由通過測試的元素組成的數組,如果沒有任何數組元素通過測試,則返回空數組。
自定義函數:myFilter。
Array.prototype.myFilter = function(callbackFn, thisArg) {
if (typeof callbackFn !== 'function') throw ('callbackFn參數必須是函數');
let element = this,
len = element && element.length || 0,
result = [];
if (!thisArg) thisArg = element;
for (let index = 0; index < len; index++) {
if (callbackFn.call(thisArg, element[index], index, element)) result.push(element[index]);
}
return result;
};語法: arr.find(callbackFn[, thisArg])
方法功能: 返回數組中滿足提供的測試函數的第一個元素的值。否則返回 undefined。
返回:數組中第一個滿足所提供測試函數的元素的值,否則返回 undefined。
自定義函數:myFind。
Array.prototype.myFind = function(callbackFn, thisArg) {
if (typeof callbackFn !== 'function') throw ('callbackFn參數必須是函數');
let element = this,
len = element && element.length || 0;
if (!thisArg) thisArg = element;
for (let index = 0; index < len; index++) {
if (callbackFn.call(thisArg, element[index], index, element)) {
return element[index];
}
}
return
}語法: arr.findIndex(callbackFn[, thisArg])
方法功能: 返回數組中滿足提供的測試函數的第一個元素的值。否則返回 undefined。
返回:數組中通過提供測試函數的第一個元素的索引。否則,返回-1。
自定義函數:myFindIndex。
Array.prototype.myFindIndex = function(callbackFn, thisArg) {
if (typeof callbackFn !== 'function') throw ('callbackFn參數必須是函數');
let element = this,
len = element && element.length || 0;
if (!thisArg) thisArg = element;
for (let index = 0; index < len; index++) {
if (callbackFn.call(thisArg, element[index], index, element)) return index;
}
return -1;
}語法: arr.fill(value[, start[, end]])
方法功能: 用一個固定值填充一個數組中從起始索引到終止索引內的全部元素。不包括終止索引。
返回:返回替換的值,原數組發生改變。
自定義函數:myFill。
Array.prototype.myFill = function(value, start = 0, end) {
let element = this,
len = element && element.length || 0;
end = end || len;
let loopStart = start < 0 ? 0 : start, // 設置循環開始值
loopEnd = end >= len ? len : end; // 設置循環結束值
for (; loopStart < loopEnd; loopStart++) {
element[loopStart] = value;
}
return element;
}語法: var new_array = arr.map(function callbackFn(currentValue[, index[, array]]) {// Return element for new_array }[, thisArg])
方法功能: 創建一個新數組,其結果是該數組中的每個元素是調用一次提供的函數后的返回值。
返回:測試數組中是不是至少有1個元素通過了被提供的函數測試。它返回的是一個Boolean類型的值。 一個由原數組每個元素執行回調函數的結果組成的新數組。
自定義函數:myMap。
Array.prototype.myMap = function(callbackFn, thisArg) {
if (typeof callbackFn !== 'function') throw ('callbackFn參數必須是函數');
let element = this,
len = element && element.length || 0,
result = [];
if (!thisArg) thisArg = element;
for (let index = 0; index < len; index++) {
result[index] = callbackFn.call(thisArg, element[index], index, element);
}
return result;
}語法: arr.some(callbackFn(currentValue[, index[, array]])[, thisArg])
方法功能: 測試數組中是不是至少有1個元素通過了被提供的函數測試。它返回的是一個Boolean類型的值。
返回:數組中有至少一個元素通過回調函數的測試就會返回true;所有元素都沒有通過回調函數的測試返回值才會為false。
自定義函數:mySome。
Array.prototype.mySome = function(callbackFn, thisArg) {
if (typeof callbackFn !== 'function') throw ('callbackFn參數必須是函數');
let element = this,
len = element && element.length || 0;
if (!thisArg) thisArg = element;
for (let index = 0; index < len; index++) {
if (callbackFn.call(thisArg, element[index], index, element)) return true;
}
return false;
}語法: arr.every(callbackFn(currentValue[, index[, array]])[, thisArg])
方法功能 :測試一個數組內的所有元素是否都能通過某個指定函數的測試。它返回一個布爾值。
返回:如果回調函數的每一次返回都為 true 值,返回 true,否則返回 false。
自定義函數:myEvery。
Array.prototype.myEvery = function(callbackFn, thisArg) {
if (typeof callbackFn !== 'function') throw ('callbackFn參數必須是函數');
let element = this,
len = element && element.length || 0;
if (!thisArg) thisArg = element;
for(let index = 0; index < len; index++) {
if (!callbackFn.call(thisArg, element[index], index, element)) return false;
}
return true;
}語法: arr.reduce(callback(accumulator, currentValue[, index[, array]])[, initialValue])
方法功能: 對數組中的每個元素執行一個由您提供的reducer函數(升序執行),將其結果匯總為單個返回值。
返回:函數累計處理的結果。
自定義函數:myReduce。
Array.prototype.myReduce = function(callbackFn, initialValue) {
if (typeof callbackFn !== 'function') throw ('callbackFn參數必須是函數');
let element = this,
len = element.length || 0,
index = 0,
result;
if (arguments.length >= 2) {
result = arguments[1];
} else {
while (index < len && !(index in element)) index++;
if (index >= len) throw new TypeError('Reduce of empty array ' + 'with no initial value');
result = element[index++];
}
while (index < len) {
if (index in element) result = callbackFn(result, element[index], index, element);
index++;
}
return result;
}語法: arr.reduceRight(callback(accumulator, currentValue[, index[, array]])[, initialValue])
方法功能: 接受一個函數作為累加器(accumulator)和數組的每個值(從右到左)將其減少為單個值。
返回:執行之后的返回值。
自定義函數:myReduceRight。
Array.prototype.myReduceRight = function(callbackFn, initialValue) {
if (typeof callbackFn !== 'function') throw ('callbackFn參數必須是函數');
let element = this,
len = element.length || 0,
index = len - 1,
result;
if (arguments.length >= 2) {
result = arguments[1];
} else {
while (index >= 0 && !(index in element)) {
index--;
}
if (index < 0) {
throw new TypeError('reduceRight of empty array with no initial value');
}
result = element[index--];
}
for (; index >= 0; index--) {
if (index in element) {
result = callbackFn(result, element[index], index, element);
}
}
return result;
}看完上述內容,你們掌握JavaScript中數組的函數有哪些的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。