javascript中怎么使用reduce方法

发布时间:2021-06-11 10:07:48 作者:小新
来源:亿速云 阅读:142

这篇文章主要为大家展示了“javascript中怎么使用reduce方法”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“javascript中怎么使用reduce方法”这篇文章吧。

在javascript中,reduce是归并方法,语法格式为“数组.reduce(function(前一个值,当前值,索引,数组对象){},初始值)”。reduce方法接收一个函数作为累加器,数组中的每个值开始缩减,最终计算为一个值。

本教程操作环境:windows7系统、javascript1.8.5版、Dell G3电脑。

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

reduce 接收两个参数:

第一个参数是在每一项上调用的函数

该函数接收 4 个参数:

第二个可选参数是作为归并基础的初始值

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

代码表示:

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

归并

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

举例说明

对数组 [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;
};

以上是“javascript中怎么使用reduce方法”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

推荐阅读:
  1. javascript中reduce如何使用
  2. JavaScript中Reduce()方法的使用是什么

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

javascript reduce

上一篇:javascript中怎么创建数组

下一篇:怎么使用PHP正则对表单数据进行验证

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》