Answers for "reduce is a array method?"

90

javascript reduce

var array = [36, 25, 6, 15];

array.reduce(function(accumulator, currentValue) {
  return accumulator + currentValue;
}, 0); // 36 + 25 + 6 + 15 = 82
Posted by: Guest on May-21-2020
2

javascript reduce

/* reduce method: combine an array's values together, left to right */

let myArray = [1, 2, 3];

let a = myArray.reduce((initial, current) => initial + current, 0);
// [0 + 1 + 2 + 3] → 6

let b = myArray.reduce((initial, current) => initial + (current*10), 7);
// [7 + (1*10) + (2*10) + (3*10)] → 67

// Combine right to left with .reduceRight
let c = myArray.reduceRight((initial, current) => initial / current, 30);
// [30 / 3 / 2 / 1] → 5

/* Syntax: array.reduce((accumulator, currentValue) =>
combineFunction, accumulatorStartValue); */
Posted by: Guest on March-05-2022
2

reduce method

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

The reducer function takes four arguments:
Accumulator (acc)
Current Value (cur)
Current Index (idx)
Source Array (src)

//syntax
arr.reduce(callback( accumulator, currentValue, [, index[, array]] )[, initialValue])
//example flatten an array

let flattened = [[0, 1], [2, 3], [4, 5]].reduce(
  ( accumulator, currentValue ) => accumulator.concat(currentValue),
  []
)
Posted by: Guest on September-09-2020

Code answers related to "Javascript"

Browse Popular Code Answers by Language