Arrays JavaScript : Înțelegerea funcției REDUCE

Sintaxă

arr.reduce(callbackFunction, [initialValue])

Ce se reduce face?

Reduce este o funcție care buclele printr-o matrice.

Operațiunile pot fi efectuate pe matrice printr-o funcție de apel invers.

La sfârșitul operațiilor pe matrice, reducerea se întoarce o singură valoare

Reduce can be used, de exemplu, to total a shopping cart

Exemplu

  lasa arr = [1,2,3,4,5];
  arr.reduce( (acumulator, currentValue, currentIndex, array) => { 
      //some operations;
      return something;
  });

 

The Callback Function

The callback function is invoked for each element in the array, passing in 4 arguments which are passed in by the reduce function.

These 4 arguments then can be used in our callback function.

Takes four arguments

  1. acumulator – The first element of the array on the first loop or initial value if supplied. Then the returned value of the previous loop.
  2. currentValue – The current element in the array. 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 – The index of the current element.
  4. array – The whole array.

Working Example

lasa arr = [1,2,3,4,5];

arr.reduce(
  (acumulator, currentValue, currentIndex, array) => {
    console.log(`accumulator (On 1st loop, accumulator is arr[0], then it is the returned value of the previous loop): ${acumulator}`);
    console.log(`currentValue (Loops through the array, starting at arr[1]): ${currentValue}`);
    console.log(`currentIndex: ${currentIndex}`);
    console.log(`array: ${array}`);
    console.log('---');
    return accumulator + currentValue;
  }
);

producție

acumulator (On 1st loop, accumulator is arr[0], then it is the returned value of the previous loop): 1
currentValue (Loops through the array, starting at arr[1]): 2
currentIndex: 1
array: 1,2,3,4,5
---
acumulator (On 1st loop, accumulator is arr[0], then it is the returned value of the previous loop): 3
currentValue (Loops through the array, starting at arr[1]): 3
currentIndex: 2
array: 1,2,3,4,5
---
acumulator (On 1st loop, accumulator is arr[0], then it is the returned value of the previous loop): 6
currentValue (Loops through the array, starting at arr[1]): 4
currentIndex: 3
array: 1,2,3,4,5
---
acumulator (On 1st loop, accumulator is arr[0], then it is the returned value of the previous loop): 10
currentValue (Loops through the array, starting at arr[1]): 5
currentIndex: 4
array: 1,2,3,4,5
---

 

Providing the initial value

Specifying the second parameter (valoarea initiala) 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

Working Example

  lasa arr = [1,2,3,4,5];

  arr.reduce(
  (acumulator, currentValue, currentIndex, array) => {
    console.log(`accumulator ( On the 1st loop, this is the initial value, then the returned value of the previous loop): ${acumulator}`);
    console.log(`currentValue (Loops through the array, starting at arr[0]): ${currentValue}`);
    console.log(`currentIndex: ${currentIndex}`);
    console.log(`array: ${array}`);
    console.log('---');
    return accumulator + currentValue;
  }, 0
);

producție

acumulator ( On the 1st loop, this is the initial value, then the returned value of the previous loop): 0
currentValue (Loops through the array, starting at arr[0]): 1
currentIndex: 0
array: 1,2,3,4,5
---
acumulator ( On the 1st loop, this is the initial value, then the returned value of the previous loop): 1
currentValue (Loops through the array, starting at arr[0]): 2
currentIndex: 1
array: 1,2,3,4,5
---
acumulator ( On the 1st loop, this is the initial value, then the returned value of the previous loop): 3
currentValue (Loops through the array, starting at arr[0]): 3
currentIndex: 2
array: 1,2,3,4,5
---
acumulator ( On the 1st loop, this is the initial value, then the returned value of the previous loop): 6
currentValue (Loops through the array, starting at arr[0]): 4
currentIndex: 3
array: 1,2,3,4,5
---
acumulator ( On the 1st loop, this is the initial value, then the returned value of the previous loop): 10
currentValue (Loops through the array, starting at arr[0]): 5
currentIndex: 4
array: 1,2,3,4,5
---