• 11 # 手写 reduce 方法


    reduce 使用

    reduce() 方法对数组中的每个元素按序执行一个提供的 reducer 函数,每一次运行 reducer 会将先前元素的计算结果作为参数传入,最后将其结果汇总为单个返回值。

    第一次执行回调函数时,不存在“上一次的计算结果”。如果需要回调函数从数组索引为 0 的元素开始执行,则需要传递初始值。否则,数组索引为 0 的元素将被用作初始值,迭代器将从第二个元素开始执行(即从索引为 1 而不是 0 的位置开始)。

    • callbackfn 函数
      • accumulator:上一次调用 callbackFn 的结果。在第一次调用时,如果指定了 initialValue 则为指定的值,否则为 array[0] 的值。
      • currentValue:当前元素的值。在第一次调用时,如果指定了 initialValue,则为 array[0] 的值,否则为 array[1]
      • currentIndex:currentValue 在数组中的索引位置。在第一次调用时,如果指定了 initialValue 则为 0,否则为 1。
      • array:调用了 reduce() 的数组本身。
    • initialValue(可选):第一次调用回调时初始化 accumulator 的值。如果指定了 initialValue,则 callbackFn 从数组中的第一个值作为 currentValue 开始执行。如果没有指定 initialValue,则 accumulator 初始化为数组中的第一个值,并且 callbackFn 从数组中的第二个值作为 currentValue 开始执行。在这种情况下,如果数组为空(没有第一个值可以作为 accumulator 返回),则会抛出错误(Uncaught TypeError: Reduce of empty array with no initial value)。
    <script>
        var arr = [1, 2, 3, 4];
        var result = arr.reduce(function (accumulator, currentValue, currentIndex, array) {
            console.log("accumulator----->", accumulator);
            console.log("currentValue----->", currentValue);
            console.log("currentIndex----->", currentIndex);
            console.log("array----->", array);
            return accumulator + currentValue;
        }, -3);
        console.warn("result----->", result);
    script>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    手写 reduce

    <script>
        Array.prototype.kaimoReduce = function (callbackfn, initialValue) {
            let hasInitialValue = initialValue !== undefined;
            if (this.length === 0) {
                if (hasInitialValue) {
                    return initialValue;
                } else {
                    throw TypeError("Reduce of empty array with no initial value");
                }
            }
            let accumulator = hasInitialValue ? initialValue : this[0];
            let i = hasInitialValue ? 0 : 1;
            for (i; i < this.length; i++) {
                accumulator = callbackfn(accumulator, this[i], i, this);
            }
            return accumulator;
        };
    
        var result2 = arr.kaimoReduce(function (accumulator, currentValue, currentIndex, array) {
            console.log("accumulator---kaimoReduce-->", accumulator);
            console.log("currentValue---kaimoReduce-->", currentValue);
            console.log("currentIndex---kaimoReduce-->", currentIndex);
            console.log("array---kaimoReduce-->", array);
            return accumulator + currentValue;
        }, -3);
    
        console.warn("result2---kaimoReduce-->", result2);
    script>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    在这里插入图片描述
    数组为空数组以及没有初始值的时候,就直接报错提示

    在这里插入图片描述

  • 相关阅读:
    【ALM工具软件】上海道宁与Perforce为您带来用于整个生命周期的应用程序生命周期管理软件
    仿牛客网项目第五,六章:异步消息系统和分布式搜索引擎(详细步骤和思路)
    k8s资源对象(一)
    Linux:在线扩容
    如此编码 202201-1 【CSP】
    java计算机毕业设计ssm健达企业项目管理系统(源码+系统+mysql数据库+Lw文档)
    Linux本地部署开源AI的PDF工具—Stirling PDF并实现公网随时访问
    深度网络架构的设计技巧(二)之BoT:Bottleneck Transformers for Visual Recognition
    振弦采集模块的信号检测与分析计算
    nodejs安装教程Windows版本
  • 原文地址:https://blog.csdn.net/kaimo313/article/details/134318699