JavaScript手寫數(shù)組的常用函數(shù)總結
在開發(fā)過程中,我們常常使用數(shù)組的一些 api 相關操作,其中包含 forEach 、 filter 、 find 、 findIndex 、 map 、 some 、 every 、 reduce 、 reduceRight 等函數(shù)方法。
今天,我們試試手寫這些函數(shù),實現(xiàn)數(shù)組這些函數(shù)方法。為了方便,直接在數(shù)組原型對象 prototype 上擴展。
本文 Githab 已上傳,更多往期文章已分類整理。
正文
參數(shù)說明callbackFn 回調(diào)函數(shù)
thisArg 執(zhí)行 callbackFn 時使用的 this 值
currentValue 數(shù)組中正在處理的元素
index 當前索引
array 源數(shù)組
accumulator 累加器
initialValue reduce reduceRight 第一次調(diào)用 callbackFn 函數(shù)時的第一個參數(shù)的值默認值
element 自己實現(xiàn)的 this 對象
forEach 函數(shù)語法: arr.forEach(callbackFn(currentValue [, index [, array]])[, thisArg])
方法功能: 對數(shù)組的每個元素執(zhí)行一次給定的函數(shù)。
返回:undefined。
自定義函數(shù):myForEach。
Array.prototype.myForEach = function(callbackFn, thisArg) { if (typeof callbackFn !== ’function’) throw (’callbackFn參數(shù)必須是函數(shù)’); 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); }};filter 函數(shù)
語法: var newArray = arr.filter(callbackFn(currentValue[, index[, array]])[, thisArg])
方法功能: 創(chuàng)建一個新數(shù)組, 其包含通過所提供函數(shù)實現(xiàn)的測試的所有元素。
返回:一個新的、由通過測試的元素組成的數(shù)組,如果沒有任何數(shù)組元素通過測試,則返回空數(shù)組。
自定義函數(shù):myFilter。
Array.prototype.myFilter = function(callbackFn, thisArg) { if (typeof callbackFn !== ’function’) throw (’callbackFn參數(shù)必須是函數(shù)’); 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;};find 函數(shù)
語法: arr.find(callbackFn[, thisArg])
方法功能: 返回數(shù)組中滿足提供的測試函數(shù)的第一個元素的值。否則返回 undefined。
返回:數(shù)組中第一個滿足所提供測試函數(shù)的元素的值,否則返回 undefined。
自定義函數(shù):myFind。
Array.prototype.myFind = function(callbackFn, thisArg) { if (typeof callbackFn !== ’function’) throw (’callbackFn參數(shù)必須是函數(shù)’); 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}findIndex 函數(shù)
語法: arr.findIndex(callbackFn[, thisArg])
方法功能: 返回數(shù)組中滿足提供的測試函數(shù)的第一個元素的值。否則返回 undefined。
返回:數(shù)組中通過提供測試函數(shù)的第一個元素的索引。否則,返回-1。
自定義函數(shù):myFindIndex。
Array.prototype.myFindIndex = function(callbackFn, thisArg) { if (typeof callbackFn !== ’function’) throw (’callbackFn參數(shù)必須是函數(shù)’); 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;}fill函數(shù)
語法: arr.fill(value[, start[, end]])
方法功能: 用一個固定值填充一個數(shù)組中從起始索引到終止索引內(nèi)的全部元素。不包括終止索引。
返回:返回替換的值,原數(shù)組發(fā)生改變。
自定義函數(shù):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, // 設置循環(huán)開始值 loopEnd = end >= len ? len : end; // 設置循環(huán)結束值 for (; loopStart < loopEnd; loopStart++) { element[loopStart] = value; } return element;}map 函數(shù)
語法: var new_array = arr.map(function callbackFn(currentValue[, index[, array]]) {// Return element for new_array }[, thisArg])
方法功能: 創(chuàng)建一個新數(shù)組,其結果是該數(shù)組中的每個元素是調(diào)用一次提供的函數(shù)后的返回值。
返回:測試數(shù)組中是不是至少有1個元素通過了被提供的函數(shù)測試。它返回的是一個Boolean類型的值。 一個由原數(shù)組每個元素執(zhí)行回調(diào)函數(shù)的結果組成的新數(shù)組。
自定義函數(shù):myMap。
Array.prototype.myMap = function(callbackFn, thisArg) { if (typeof callbackFn !== ’function’) throw (’callbackFn參數(shù)必須是函數(shù)’); 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;}some 函數(shù)
語法: arr.some(callbackFn(currentValue[, index[, array]])[, thisArg])
方法功能: 測試數(shù)組中是不是至少有1個元素通過了被提供的函數(shù)測試。它返回的是一個Boolean類型的值。
返回:數(shù)組中有至少一個元素通過回調(diào)函數(shù)的測試就會返回true;所有元素都沒有通過回調(diào)函數(shù)的測試返回值才會為false。
自定義函數(shù):mySome。
Array.prototype.mySome = function(callbackFn, thisArg) { if (typeof callbackFn !== ’function’) throw (’callbackFn參數(shù)必須是函數(shù)’); 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;}every 函數(shù)
語法: arr.every(callbackFn(currentValue[, index[, array]])[, thisArg])
方法功能 :測試一個數(shù)組內(nèi)的所有元素是否都能通過某個指定函數(shù)的測試。它返回一個布爾值。
返回:如果回調(diào)函數(shù)的每一次返回都為 true 值,返回 true,否則返回 false。
自定義函數(shù):myEvery。
Array.prototype.myEvery = function(callbackFn, thisArg) { if (typeof callbackFn !== ’function’) throw (’callbackFn參數(shù)必須是函數(shù)’); 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;}reduce 函數(shù)
語法: arr.reduce(callback(accumulator, currentValue[, index[, array]])[, initialValue])
方法功能: 對數(shù)組中的每個元素執(zhí)行一個由您提供的reducer函數(shù)(升序執(zhí)行),將其結果匯總為單個返回值。
返回:函數(shù)累計處理的結果。
自定義函數(shù):myReduce。
Array.prototype.myReduce = function(callbackFn, initialValue) { if (typeof callbackFn !== ’function’) throw (’callbackFn參數(shù)必須是函數(shù)’); 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;}reduceRight 函數(shù)
語法: arr.reduceRight(callback(accumulator, currentValue[, index[, array]])[, initialValue])
方法功能: 接受一個函數(shù)作為累加器(accumulator)和數(shù)組的每個值(從右到左)將其減少為單個值。
返回:執(zhí)行之后的返回值。
自定義函數(shù):myReduceRight。
Array.prototype.myReduceRight = function(callbackFn, initialValue) { if (typeof callbackFn !== ’function’) throw (’callbackFn參數(shù)必須是函數(shù)’); 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手寫數(shù)組常用函數(shù)總結的文章就介紹到這了,更多相關JS手寫數(shù)組常用函數(shù)內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持好吧啦網(wǎng)!
相關文章:
