11 #Handwritten reduce method

reduce use

The reduce() method sequentially executes a provided reducer function on each element in the array. Each time the reducer is run, the calculation results of the previous elements are passed in as parameters, and finally the results are summarized into a single return value.

When the callback function is executed for the first time, there is no "last calculation result". If you need the callback function to start executing from the element with array index 0, you need to pass an initial value. Otherwise, the element at array index 0 will be used as the initial value and the iterator will execute starting from the second element (i.e. starting at index 1 instead of 0).

  • callbackfn function
    • accumulator: The result of the last call to callbackFn. On the first call, the specified value if initialValue is specified, otherwise the value of array[0].
    • currentValue: The value of the current element. On the first call, the value of array[0] if initialValue is specified, otherwise array[1].
    • currentIndex: The index position of currentValue in the array. On the first call, 0 if initialValue is specified, 1 otherwise.
    • array: The array itself on which reduce() was called.
  • initialValue (optional): The value to initialize the accumulator when the callback is first called. If initialValue is specified, callbackFn starts executing with the first value in the array as currentValue. If initialValue is not specified, accumulator is initialized to the first value in the array, and callbackFn starts executing with the second value in the array as currentValue. In this case, if the array is empty (no first value to return as accumulator), an error (Uncaught TypeError: Reduce of empty array with no initial value) will be thrown.
<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>

handwritten 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>

Insert image description here
When the array is an empty array and has no initial value, an error message will be reported directly.

Insert image description here

Guess you like

Origin blog.csdn.net/kaimo313/article/details/134318699