我看着MDN文檔,手寫了幾個數組實例方法

語言: CN / TW / HK

theme: fancy highlight: atom-one-light


持續創作,加速成長!這是我參與「掘金日新計劃 · 6 月更文挑戰」的第29天,點擊查看活動詳情

Hi~,我是一碗周,如果寫的文章有幸可以得到你的青睞,萬分有幸~

🍊 寫在前面

手寫JS原生API在面試中很常見,今天努力工作之餘(摸魚的時候)翻到了MDN文章中關於數組實例方法這部分,正好無聊就手寫幾個實例方法玩玩,複習一下基礎內容,並記錄一下。

wolai-minder-bnb37bcPyseaQgUJyTG6S4-a52X2wzr2DFkqe.png

如果你還不知道數組實例中迭代方法有什麼區別,可以看下面這張圖:

map_filter_reduce_mDwI7IUidr.png

上圖來源自網絡,侵刪。

🍍 map

這個方法會返回一個新的數組,數組中的每一項都是執行過map提供的回調函數結果。實現代碼如下:

```javascript 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')

// 定義一個空數組,用於存放修改後的數據 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

這個方法會返回一個新的數組,數組中的值是滿足filter提供的回調函數的值,實現代碼如下:

```javascript 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')

// 定義一個空數組,用於存放符合條件的數組項 let res = [] for (let i = 0; i < array.length; i++) { // 將數組中的每一項都調用傳入的函數,如果返回結果為true,則將結果push進數組,最後返回 fun(array[i]) && res.push(array[i]) } return res } // 測試 let res = filter([1, 2, 3], item => { return item > 2 }) console.log(res) // [ 3 ]

```

🥭 some

該方法會判斷數組中的每一項,如果有一項滿足回調函數中條件就返回true都不滿足則返回false。實現代碼如下:

```javascript 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

該方法會判斷數組中的每一項,如果所有項滿足回調函數中條件就返回true否則返回false。實現代碼如下:

```javascript 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

該方法會讓數組中的每個元素執行我們提供的回調函數,並將結果彙總返回,實現代碼如下:

```javascript 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

這個方法比較簡答了,就是遍歷數組方法,數組中的每一項都執行回調函數,實現代碼如下:

```javascript 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

這兩個方法比較類似,一個返回元素,一個返回元素的索引,這裏就編寫一個,實現代碼如下:

```javascript 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

該方法可以將數組中的所有元素根據指定的字符串進行拼接,並返回拼接後的字符串,實現代碼如下:

```javascript 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

```

🍅 寫在最後

這裏手寫了數組實例中的9個方法,總體沒有多少代碼,有些也不夠完善,編寫這些方法的目的是對js基礎的一個練習。