JavaScript數(shù)組實例的9個方法
前言
手寫JS原生API在面試中很常見,今天努力工作之余(摸魚的時候)翻到了MDN文章中關(guān)于數(shù)組實例方法這部分,正好無聊就手寫幾個實例方法玩玩,復(fù)習(xí)一下基礎(chǔ)內(nèi)容,并記錄一下。
如果你還不知道數(shù)組實例中迭代方法有什么區(qū)別,可以看下面這張圖:
map
這個方法會返回一個新的數(shù)組,數(shù)組中的每一項都是執(zhí)行過map
提供的回調(diào)函數(shù)結(jié)果。
實現(xiàn)代碼如下:
const map = (array, fun) => { // 類型約束 if (Object.prototype.toString.call(array) !== '[object Array]') throw new TypeError(array + ' is not a array') if (typeof fun !== 'function') throw new TypeError(fun + ' is not a function') // 定義一個空數(shù)組,用于存放修改后的數(shù)據(jù) let res = [] for (let i = 0; i < array.length; i++) { res.push(fun(array[i])) } return res } // 測試 let res = map([1, 2, 3], item => { return item * 2 }) console.log(res) // [ 2, 4, 6 ]
filter
這個方法會返回一個新的數(shù)組,數(shù)組中的值是滿足filter
提供的回調(diào)函數(shù)的值,
實現(xiàn)代碼如下:
const filter = (array, fun) => { // 類型約束 if (Object.prototype.toString.call(array) !== '[object Array]') throw new TypeError(array + ' is not a array') if (typeof fun !== 'function') throw new TypeError(fun + ' is not a function') // 定義一個空數(shù)組,用于存放符合條件的數(shù)組項 let res = [] for (let i = 0; i < array.length; i++) { // 將數(shù)組中的每一項都調(diào)用傳入的函數(shù),如果返回結(jié)果為true,則將結(jié)果push進(jìn)數(shù)組,最后返回 fun(array[i]) && res.push(array[i]) } return res } // 測試 let res = filter([1, 2, 3], item => { return item > 2 }) console.log(res) // [ 3 ]
some
該方法會判斷數(shù)組中的每一項,如果有一項滿足回調(diào)函數(shù)中條件就返回true
都不滿足則返回false
。
實現(xiàn)代碼如下:
const some = (array, fun) => { // 類型約束 if (Object.prototype.toString.call(array) !== '[object Array]') throw new TypeError(array + ' is not a array') if (typeof fun !== 'function') throw new TypeError(fun + ' is not a function') let flag = false for (let i of array) { if (fun(i)) { flag = true break } } return flag } let res = some([1, 2, 3], item => { return item > 2 }) console.log(res) // true
every
該方法會判斷數(shù)組中的每一項,如果所有項滿足回調(diào)函數(shù)中條件就返回true
否則返回false
。
實現(xiàn)代碼如下:
const every = (array, fun) => { // 類型約束 if (Object.prototype.toString.call(array) !== '[object Array]') throw new TypeError(array + ' is not a array') if (typeof fun !== 'function') throw new TypeError(fun + ' is not a function') let flag = true for (let i of array) { if (!fun(i)) { flag = false break } } return flag } let res = every([1, 2, 3], item => { return item > 0 }) console.log(res) // true
reduce
該方法會讓數(shù)組中的每個元素執(zhí)行我們提供的回調(diào)函數(shù),并將結(jié)果匯總返回,實現(xiàn)代碼如下:
const reduce = (array, fun, initialValue) => { // 類型約束 if (Object.prototype.toString.call(array) !== '[object Array]') throw new TypeError(array + ' is not a array') if (typeof fun !== 'function') throw new TypeError(fun + ' is not a function') let accumulator = initialValue for (let i = 0; i < array.length; i++) { accumulator = fun(accumulator, array[i], i, array) } return accumulator } const arr = [1, 2, 3] console.log(arr.reduce(v => v + 10, 10)) // 40 console.log(reduce(arr, v => v + 10, 10)) // 40
forEach
這個方法比較簡答了,就是遍歷數(shù)組方法,數(shù)組中的每一項都執(zhí)行回調(diào)函數(shù),實現(xiàn)代碼如下:
const forEach = (array, fun) => { // 類型約束 if (Object.prototype.toString.call(array) !== '[object Array]') throw new TypeError(array + ' is not a array') if (typeof fun !== 'function') throw new TypeError(fun + ' is not a function') for (let i of array) { fun(i) } } let res = forEach([1, 2, 3], item => { console.log(item) })
find和findIndex
這兩個方法比較類似,一個返回元素,一個返回元素的索引,這里就編寫一個,實現(xiàn)代碼如下:
const myFind = (array, fun) => { // 類型約束 if (Object.prototype.toString.call(array) !== '[object Array]') throw new TypeError(array + ' is not a array') if (typeof fun !== 'function') throw new TypeError(fun + ' is not a function') let res for (let i = 0; i < array.length; i++) { if (fun(array[i])) { res = array[i] } } return res } // 測試 let res = myFind([1, 2, 3], item => { return item > 2 }) console.log(res) // 3
join
該方法可以將數(shù)組中的所有元素根據(jù)指定的字符串進(jìn)行拼接,并返回拼接后的字符串,
實現(xiàn)代碼如下:
const join = (array, separator = ',') => { // 類型約束 if (Object.prototype.toString.call(array) !== '[object Array]') throw new TypeError(array + ' is not a array') if (typeof separator !== 'string') throw new TypeError(separator + ' is not a string') let res = array[0].toString() for (let i = 0; i < array.length - 1; i++) { res += separator + array[i + 1].toString() } return res } // 測試 let res = join([1, 2, 3], '-') console.log(res) // 1-2-3
總結(jié)
這里手寫了數(shù)組實例中的9個方法,總體沒有多少代碼,有些也不夠完善,編寫這些方法的目的是對js基礎(chǔ)的一個練習(xí)。
到此這篇關(guān)于JavaScript數(shù)組實例的9個方法的文章就介紹到這了,更多相關(guān)JS數(shù)組實例方法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
js中使用DOM復(fù)制(克隆)指定節(jié)點名數(shù)據(jù)到新的XML文件中的代碼
使用DOM復(fù)制(克?。┲付ü?jié)點名數(shù)據(jù)到新的XML文件中 ,用到三個類的相關(guān)知識點 : DOMDocument - DOMNodeList - DOMNode2011-07-07javascript自適應(yīng)寬度的瀑布流實現(xiàn)思路
這里主要介紹瀑布流的一種實現(xiàn)方法:絕對定位(css)+javascript+ajax+json。簡單一點如果不做滾動加載的話就是絕對定位(css)+javascript了,ajax和json是滾動加載更多內(nèi)容的時候用到的,感興趣的你可以參考下哦2013-02-02小程序getLocation需要在app.json中聲明permission字段
這篇文章主要介紹了小程序getLocation需要在app.json中聲明permission字段,個別需要獲取用戶地理位置的在開發(fā)者工具調(diào)試時會出現(xiàn)getLocation需要在app.json中聲明permission字段 ,下面我們就一起來解決一下2019-04-04微信小程序?qū)崿F(xiàn)image組件圖片自適應(yīng)寬度比例顯示的方法
這篇文章主要介紹了微信小程序?qū)崿F(xiàn)image組件圖片自適應(yīng)寬度比例顯示的方法,簡單講述了image組件的常用屬性,并結(jié)合實例形式分析了微信小程序?qū)崿F(xiàn)圖片自適應(yīng)寬度比例的相關(guān)操作技巧,需要的朋友可以參考下2018-01-01