javascript —— 高階函數

定義

如果一個函數就可以接收另一個函數作爲參數,則這種函數就稱之爲高階函數

示例:

// 示例高階函數
function add(a, b, func) {
    return func(a) + func(b)
}

let kkm = add(-1, 2, Math.abs)
console.log(kkm) // 3

常見的數組高階函數

1,forEach

forEach我們都不陌生了,可以通過這個函數對數組的所有元素進行遍歷

語法:

array.forEach(function(currentValue, index, arr), thisValue)

參數:

參數 描述
function(currentValue, index, arr) 必需。 數組中每個元素需要調用的函數。
thisValue 可選。傳遞給函數的值一般用 “this” 值。如果這個參數爲空, “undefined” 會傳遞給 “this” 值

function(currentValue, index, arr)參數:

參數 描述
currentValue 必需。當前正在處理的元素
index 可選。當前元素的索引值
arr 可選。當前元素所屬的數組對象

返回值:
undefined

JavaScript 版本:
ECMAScript 5

示例:

let arr = ['kkm', 'ig', 'rng']
arr.forEach(function(val, index, arr) {
    console.log(index, val, arr)
})
/*
輸出:
0 kkm [ 'kkm', 'ig', 'rng' ]
1 ig [ 'kkm', 'ig', 'rng' ]
2 rng [ 'kkm', 'ig', 'rng' ]
*/

2,map

map函數可以將數組元素通過回調函數的處理,返回一個新的數組,原數組保持不變

語法:

array.map(function(currentValue,index,arr), thisValue)

參數:

參數 描述
function(currentValue, index, arr) 必須。函數,數組中的每個元素都會執行這個函數。
thisValue 可選。對象作爲該執行回調時使用,傳遞給函數,用作 “this” 的值。如果省略了 thisValue,或者傳入 null、undefined,那麼回調函數的 this 爲全局對象

function(currentValue, index, arr)參數:

參數 描述
currentValue 必須。當前元素的值
index 可選。當前元素的索引值
arr 可選。當前元素所屬的數組對象

返回值:
返回一個新數組,數組中的元素爲原始數組元素調用函數處理後的值

JavaScript 版本:
ECMAScript 5

示例:

let arr = [1, 2, 3]
let newArr = arr.map(function(val, index, arr) {
    return val * 10
})
console.log(arr) // [ 1, 2, 3 ]
console.log(newArr) // [ 10, 20, 30 ]

3,filter

創建一個新的數組,新數組中的元素是通過檢查指定數組中符合條件的所有元素。不會對原數組進行改變

語法:

array.filter(function(currentValue,index,arr), thisValue)

參數:

參數 描述
function(currentValue, index, arr) 必須。函數,數組中的每個元素都會執行這個函數。
thisValue 可選。對象作爲該執行回調時使用,傳遞給函數,用作 “this” 的值。如果省略了 thisValue,或者傳入 null、undefined,那麼回調函數的 this 爲全局對象

function(currentValue, index, arr)參數:

參數 描述
currentValue 必須。當前元素的值
index 可選。當前元素的索引值
arr 可選。當前元素所屬的數組對象

返回值:
返回數組,包含了符合條件的所有元素。如果沒有符合條件的元素則返回空數組

JavaScript 版本:
ECMAScript 5

示例:

let arr = [1, 3, 11, 4, 20, 1000]
let newArr = arr.filter(function(val, index, arr) {
    if (val > 10) return true
})
console.log(arr) // [ 1, 3, 11, 4, 20, 1000 ]
console.log(newArr) // [ 11, 20, 1000 ]

4,some

方法用於檢測數組中的元素是否滿足指定條件,如果有一個滿足,則返回true;否則返回false

語法:

array.some(function(currentValue,index,arr),thisValue)

參數:

參數 描述
function(currentValue, index, arr) 必須。函數,數組中的每個元素都會執行這個函數。
thisValue 可選。對象作爲該執行回調時使用,傳遞給函數,用作 “this” 的值。如果省略了 thisValue,或者傳入 null、undefined,那麼回調函數的 this 爲全局對象

function(currentValue, index, arr)參數:

參數 描述
currentValue 必須。當前元素的值
index 可選。當前元素的索引值
arr 可選。當前元素所屬的數組對象

返回值:
布爾值。如果數組中有元素滿足條件返回 true,否則返回 false

JavaScript 版本:
ECMAScript 5

示例:

let arr = [11, 13, 11, 20, 18]
let result = arr.some(function(val, index, arr) {
    return val >= 18
})
console.log(result) // true

5,every

方法用於檢測數組所有元素是否都符合指定條件,如果有一個不滿足,則返回false;否則返回true

語法:

array.every(function(currentValue,index,arr),thisValue)

參數:

參數 描述
function(currentValue, index, arr) 必須。函數,數組中的每個元素都會執行這個函數。
thisValue 可選。對象作爲該執行回調時使用,傳遞給函數,用作 “this” 的值。如果省略了 thisValue,或者傳入 null、undefined,那麼回調函數的 this 爲全局對象

function(currentValue, index, arr)參數:

參數 描述
currentValue 必須。當前元素的值
index 可選。當前元素的索引值
arr 可選。當前元素所屬的數組對象

返回值:
布爾值。如果所有元素都通過檢測返回 true,否則返回 false

JavaScript 版本:
ECMAScript 5

示例:

let arr = [11, 13, 11, 20, 18]
let result = arr.every(function(val, index, arr) {
    return val >= 18
})
console.log(result) // false

6,reduce

方法接收一個函數作爲累加器,數組中的每個值(從左到右)開始縮減,最終計算爲一個值
語法:

array.reduce(function(total, currentValue, index, arr), initialValue)

參數:

參數 描述
function(currentValue, index, arr) 必需。用於執行每個數組元素的函數
initialValue 可選。傳遞給函數的初始值

function(currentValue, index, arr)參數:

參數 描述
total 必需。初始值, 或者計算結束後的返回值
currentValue 必須。當前元素的值
index 可選。當前元素的索引值
arr 可選。當前元素所屬的數組對象

返回值:
返回計算結果

JavaScript 版本:
ECMAScript 5

示例:

var arr = [1,2,3,4]
var sum = arr.reduce(function(a,b){
    console.log(a)
	return a + b
})
console.log(sum)
/*
1
3
6
10
*/

// 當有初始值時,回調函數會多執行一次
// 可見:如果沒有初始值,數組的第一個元素沒有參與回調
var arr = [1,2,3,4]
var sum = arr.reduce(function(a,b){
    console.log(a)
	return a + b
}, 10)
console.log(sum)
/*
10
11
13
16
20
*/

7,reduceRight

功能和 reduce() 功能是一樣的,不同的是 reduceRight() 從數組的末尾向前將數組中的數組項做累加
語法:

array.reduce(function(total, currentValue, index, arr), initialValue)

參數:

參數 描述
function(currentValue, index, arr) 必需。用於執行每個數組元素的函數
initialValue 可選。傳遞給函數的初始值

function(currentValue, index, arr)參數:

參數 描述
total 必需。初始值, 或者計算結束後的返回值
currentValue 必須。當前元素的值
index 可選。當前元素的索引值
arr 可選。當前元素所屬的數組對象

返回值:
返回計算結果

JavaScript 版本:
ECMAScript 5

示例:

var arr = [1,2,3,4]
var sum = arr.reduceRight(function(a,b){
    console.log(a)
	return a + b
})
console.log(sum)
/*
4
7
9
10
*/
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章