JavaScript 实现 reduce() 方法函数 您所在的位置:网站首页 js的reduce函数 JavaScript 实现 reduce() 方法函数

JavaScript 实现 reduce() 方法函数

2024-06-13 18:58| 来源: 网络整理| 查看: 265

更新:增加递归实现的方法 更新:重构非递归实现的方法

思路

与之前两篇文章( map()的实现 ,filter()的实现 )中的迭代方法不一样,reduce() 是归并方法。

reduce 接收两个参数:

第一个参数是在每一项上调用的函数 该函数接收 4 个参数: 前一个值 prev 当前值 cur 项的索引 index 数组对象 array 第二个可选参数是作为归并基础的初始值

reduce 方法返回一个最终的值。

代码表示:

arr.reduce(function(prev, cur, index, arr){}, initialValue) 归并

与之前的迭代不同,归并不是对每一项都执行目标函数,而是可以概括为如下两步:

不断地对数组的前两项“取出”,对其执行目标函数,计算得到的返回值把上述返回值“填回”数组首部,作为新的 array[0]持续循环执行这个过程,直到数组中每一项都访问了一次返回最终结果 举例说明

对数组 [1,2,3] 归并执行 (prev, cur) => prev + cur,流程如图:

[1, 2, 3] // 取出 1 + 2 ,填回 3 [3, 3] // 取出 3 + 3 ,填回 6 [6] // 最终结果为 6

所以得到 6 。

实现 第一版

根据这个思路,得到第一版代码如下

// 第一版 Array.prototype.fakeReduce = function fakeReduce(fn, base) { // let arr = base ? this.unshift(base) : this;// 首进,返回新数组的长度,影响原数组 故不能这么写 let initialArr = this; let arr = initialArr.concat(); //得到副本 if (base) arr.unshift(base); // 当存在归并基础值的参数时,将其从数组首部推入 let index; while (arr.length > 2) { index = initialArr.length - arr.length + 1; let newValue = fn.call(null, arr[0], arr[1], index, initialArr); arr.splice(0, 2); // 删除前两项,影响原数组 arr.unshift(newValue);// 把 fn(arr[0],arr[1]) 的结果从数组首部推入 } index += 1; let result = fn.call(null, arr[0], arr[1], index, initialArr); return result; };

注意点:

队列方法 unshift()

可以从数组首部加入任意个项,返回值是新数组的长度影响原数组

splice() 方法,高程三将其誉为最强大的数组方法

删除任意数量的项 指定 2 个参数: (删除起始位置, 删除项个数) 插入任意数量的项 指定 3 个参数: (起始位置,0,要插入的项)第二个参数 0 即为要删除的个数 替换,即删除任意数量的项的同时,插入任意数量的项 指定 3 个参数:(起始位置,要删除的个数, 要插入的任意数量的项) 返回值 始终是一个数组,包含从原始数组中删除的项。若未删除任何项,返回空数组 影响原数组 改进版

从上面的总结可以看出,splice() 方法完全可以取代 unshift() 方法。

而且,第一版中存在一些重复代码,也可以改进。

由此得到第二版代码

// 第二版 Array.prototype.fakeReduce = function fakeReduce(fn, base) { let initialArr = this; let arr = initialArr.concat(); if (base) arr.unshift(base); let index, newValue; while (arr.length > 1) { index = initialArr.length - arr.length + 1; newValue = fn.call(null, arr[0], arr[1], index, initialArr); arr.splice(0, 2, newValue); // 直接用 splice 实现替换 } return newValue; };

检测:

let arr = [1, 2, 3, 4, 5]; let sum = arr.fakeReduce((prev, cur, index, arr) => { console.log(prev, cur, index, arr); return prev * cur; }, 100); console.log(sum);

输出:

100 1 0 [ 1, 2, 3, 4, 5 ] 100 2 1 [ 1, 2, 3, 4, 5 ] 200 3 2 [ 1, 2, 3, 4, 5 ] 600 4 3 [ 1, 2, 3, 4, 5 ] 2400 5 4 [ 1, 2, 3, 4, 5 ] 12000

最后加上类型检测等

// 第三版 Array.prototype.fakeReduce = function fakeReduce(fn, base) { if (typeof fn !== "function") { throw new TypeError("arguments[0] is not a function"); } let initialArr = this; let arr = initialArr.concat(); if (base) arr.unshift(base); let index, newValue; while (arr.length > 1) { index = initialArr.length - arr.length + 1; newValue = fn.call(null, arr[0], arr[1], index, initialArr); arr.splice(0, 2, newValue); // 直接用 splice 实现替换 } return newValue; }; 递归实现 简易版 const reduceHelper = (f, acc, arr) => { if (arr.length === 0) return acc const [head, ...tail] = arr return reduceHelper(f, f(acc, head), tail) } Array.prototype.fakeReduce = function (fn, initialValue) { const array = this return reduceHelper(fn, initialValue, array) }

注:acc 即 accumulator, 累计回调的返回值。它是上一次调用回调时返回的累积值或 initialValue。

升级版

支持 cb 函数的全部参数

const reduceHelper = (fn, acc, idx, array) => { if (array.length === 0) return acc const [head, ...tail] = array idx++ return reduceHelper(fn, fn(acc, head, idx, array), idx, tail) } Array.prototype.myReduce = function (cb, initialValue) { const array = this const [head, ...tail] = array const startIndex = initialValue ? -1 : 0 return initialValue ? reduceHelper(cb, initialValue, startIndex, array) : reduceHelper(cb, head, startIndex, tail) } 重构非递归 Array.prototype.myReduce = function (cb, initialValue) { const array = this let acc = initialValue || array[0] const startIndex = initialValue ? 0 : 1 for (let i = startIndex; i < array.length; i++) { const cur = array[i] acc = cb(acc, cur, i, array) } return acc }


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有