JavaScript数组 : 了解减少功能

句法

arr.reduce(callbackFunction, [initialValue])

什么是减少做?

减少是通过阵列循环函数.

操作可以在阵列上通过回调函数来执行.

在操作的阵列上的端, 减少返回单个值

Reduce can be used, 例如, to total a shopping cart

  让ARR = [1,2,3,4,5];
  arr.reduce( (累加器, 当前值, CURRENTINDEX, 排列) => { 
      //一些操作;
      返回的东西;
  });

 

回调函数

回调函数被调用用于阵列中的每个元件, 传递 4 这是通过精简函数传递参数.

这些 4 参数则可以在回调函数中使用.

有四个参数

  1. 累加器 – 如果提供在第一循环或初始值的阵列的第一个元素. 那么前一个循环的返回值.
  2. 当前值 – 阵列中的当前元素. Starts from the second element in the array ie. arr[1], when the initial value argument is not passed. If the initial value argument is passed then current value starts from the 1st element in the array arr[0].
  3. CURRENTINDEX – 当前元素的索引.
  4. 排列 – 整个阵列.

工作实例

让ARR = [1,2,3,4,5];

arr.reduce(
  (累加器, 当前值, CURRENTINDEX, 排列) => {
    的console.log(`累加器 (On 1st loop, accumulator is arr[0], then it is the returned value of the previous loop): ${累加器}`);
    的console.log(`CurrentValue的 (Loops through the array, starting at arr[1]): ${当前值}`);
    的console.log(`CURRENTINDEX: ${CURRENTINDEX}`);
    的console.log(`阵列: ${排列}`);
    的console.log('---');
    回累加器 + 当前值;
  }
);

产量

累加器 (On 1st loop, accumulator is arr[0], then it is the returned value of the previous loop): 1
当前值 (Loops through the array, starting at arr[1]): 2
CURRENTINDEX: 1
排列: 1,2,3,4,5
---
累加器 (On 1st loop, accumulator is arr[0], then it is the returned value of the previous loop): 3
当前值 (Loops through the array, starting at arr[1]): 3
CURRENTINDEX: 2
排列: 1,2,3,4,5
---
累加器 (On 1st loop, accumulator is arr[0], then it is the returned value of the previous loop): 6
当前值 (Loops through the array, starting at arr[1]): 4
CURRENTINDEX: 3
排列: 1,2,3,4,5
---
累加器 (On 1st loop, accumulator is arr[0], then it is the returned value of the previous loop): 10
当前值 (Loops through the array, starting at arr[1]): 5
CURRENTINDEX: 4
排列: 1,2,3,4,5
---

 

提供初始值

指定的第二个参数 (初始值) in the reduce function will cause initialValue to be passed in as the accumulator argument on the first loop.

Note that the current value on the 1st loop now starts at the first element of the array

工作实例

  让ARR = [1,2,3,4,5];

  arr.reduce(
  (累加器, 当前值, CURRENTINDEX, 排列) => {
    的console.log(`累加器 ( On the 1st loop, this is the initial value, then the returned value of the previous loop): ${累加器}`);
    的console.log(`CurrentValue的 (Loops through the array, starting at arr[0]): ${当前值}`);
    的console.log(`CURRENTINDEX: ${CURRENTINDEX}`);
    的console.log(`阵列: ${排列}`);
    的console.log('---');
    回累加器 + 当前值;
  }, 0
);

产量

累加器 ( On the 1st loop, this is the initial value, then the returned value of the previous loop): 0
当前值 (Loops through the array, starting at arr[0]): 1
CURRENTINDEX: 0
排列: 1,2,3,4,5
---
累加器 ( On the 1st loop, this is the initial value, then the returned value of the previous loop): 1
当前值 (Loops through the array, starting at arr[0]): 2
CURRENTINDEX: 1
排列: 1,2,3,4,5
---
累加器 ( On the 1st loop, this is the initial value, then the returned value of the previous loop): 3
当前值 (Loops through the array, starting at arr[0]): 3
CURRENTINDEX: 2
排列: 1,2,3,4,5
---
累加器 ( On the 1st loop, this is the initial value, then the returned value of the previous loop): 6
当前值 (Loops through the array, starting at arr[0]): 4
CURRENTINDEX: 3
排列: 1,2,3,4,5
---
累加器 ( On the 1st loop, this is the initial value, then the returned value of the previous loop): 10
当前值 (Loops through the array, starting at arr[0]): 5
CURRENTINDEX: 4
排列: 1,2,3,4,5
---                                                                 

发表评论