Arr. Reduce () method explanation

The Reference link

The reduce() method executes a reducer function (that you provide) on each element of the array, resulting in single output value.

That is, the arr.reduce() method handles each element of the ARR with the Reducer () function. Let’s examine the example given by Reference:

const array1 = [1.2.3.4];
const reducer = (accumulator, currentValue) = > accumulator + currentValue;

// 1 + 2 + 3 + 4
console.log(array1.reduce(reducer));
// expected output: 10

// 5 + 1 + 2 + 3 + 4
console.log(array1.reduce(reducer, 5));
// expected output: 15
Copy the code
  1. array1.reduce(reducer):reducerThis function is going to bearray1For each element of.reducerfunctionarray1Each element in the array will be placedcurrentValueAnd save the processed results inaccumulatorThe inside.
  2. reducerThis function takes two arguments, one isaccumulator, one iscurrentValue. After the Reducer has processed all elements, Accumulator is savedarray1.reduce(reducer)The final result of.
  3. array1.reduce(reducer)witharray1.reduce(reducer, 5)What is the difference between? The difference is that the latter provides an initialValue of 5, soaccumulatorThe initialValue of is equal to the initialValue,currentValueThe value is equal to thearray1The first element of (array1[0]). The former, since no initial value is provided,accumulatorThe initial value of alpha is equal to alphaarray1[0].currentValueIs equal to thearray1[1]. This is the difference between providing an initialValue or not.
  4. Accumulator is an accumulator. CurrentValue is equal to the element in the array.

Arr. Reduce () practice

Flattening

Use the reduce method in combination with the concat method to “flatten” an array of arrays into a single array that has all the elements of the original arrays. The code I wrote is:

function flatten(arr) {
    return arr.reduce((a, b) = > a.concat(b)); 
} 

let arrays = [[1.2.3], [4.5], [6]].console.log(flatten(arrays)); 
Console. log(arrays.reduce((a, b) => a.conconcat (b))));
// The output is [1, 2, 3, 4, 5, 6]
Copy the code

Arr. reduce((a, b) => a.concat(b)), function reducer = (a, b) => a.concat(b). Since no initial value is provided, the initial value of A is [1, 2, 3] and the initial value of b is [4, 5]. We can also provide an initial value (an empty array) : arr.reduce((a, b) => a.concat(b), []). So the initial value of A is [], and the initial value of b is [1, 2, 3]. If the initial value we provide is [0] : arr.reduce((a, b) => a.concat(b), [0]), then the final result is [0, 1, 2, 3, 4, 5, 6].

Array.prototype.flat()

reduce + concat + isArray + recursivity

const arr = [1.2[3.4[5.6]]];

// to enable deep level flatten use recursion with reduce and concat
function flatDeep(arr, d = 1) {
   return d > 0 ? arr.reduce((acc, val) = > acc.concat(Array.isArray(val) ? flatDeep(val, d - 1) : val), [])
                : arr.slice();
};

flatDeep(arr, Infinity);
// [1, 2, 3, 4, 5, 6]
Copy the code

Array.prototype.some()

The some() method tests whether at least one element in the array passes the test implemented by the provided function. It returns true if, in the array, it finds an element for which the provided function returns true; otherwise it returns false. It doesn’t modify the array.

const array = [1.2.3.4.5];

// checks whether an element is even
const even = (element) = > element % 2= = =0;

console.log(array.some(even));
// expected output: true
Copy the code