Javascript Reduce an empty array

JavascriptArraysEcmascript 5

Javascript Problem Overview


When I reduce the array, I am trying to get the number zero, but I dont clearly understand the behaviour of the function

[].reduce(function(previousValue, currentValue){
  return Number(previousValue) + Number(currentValue);
});

result

TypeError: Reduce of empty array with no initial value

seems that if the array is empty I can't reduce it

[""].reduce(function(previousValue, currentValue){
  return Number(previousValue) + Number(currentValue);
});

result

""

If the only element in the array is an empty string, retrieves an empty string

Javascript Solutions


Solution 1 - Javascript

The second parameter is for initial value.

[].reduce(function(previousValue, currentValue){
  return Number(previousValue) + Number(currentValue);
}, 0);

or using ES6:

[].reduce( (previousValue, currentValue) => previousValue + currentValue, 0);

Solution 2 - Javascript

Both behaviors are according to the spec.

You cannot reduce an empty array unless you explicitly provide an initial "accumulated" value as the second argument:

> If no initialValue was provided, then previousValue will be equal to > the first value in the array and currentValue will be equal to the > second. It is a TypeError if the array contains no elements and > initialValue is not provided.

If the array has at least one element then providing an initial value is optional. However, if one is not provided then the first element of the array is used as the initial value and reduce continues to process the rest of the array elements by invoking your callback. In your case the array only contains a single element, so that element becomes the initial value and also final value, since there are no more elements to be processed through the callback.

Solution 3 - Javascript

In your example, the previous value must be defined first, because it is equivalent to assigning a value to a variable previousValue

let previousValue = 0; is different from let previousValue;

Solution 4 - Javascript

Just ran into this myself and want to give this an answer with a more simple yet modern JavaScript example.

JavaScript's array reduce function iterates over an array (here list) and reduces it to a new result (here sum) by computing each item (here value) in the array with the accumulator (here acc):

const sum = list.reduce((acc, value) => acc + value);

Most reduce operations prefer to operate on an initial accumulator (second argument, also called initial value). In our example, it's indeed even mandatory, because otherwise the sum operation wouldn't work. We want to sum our values with an initial value of 0:

const sum = list.reduce((acc, value) => acc + value, 0);

The initial value for a reduce function is its second argument (here 0). So if you come across the error:

TypeError: Reduce of empty array with no initial value

It just means that you have to provide an initial (empty) array for your reduce operation, because if you provide an empty array (here list) to start with, it doesn't work and complains with this error. In order to fix it, provide an empty array as second argument:

const exponentials = list.reduce((acc, value) => {
  acc = acc.concat(value * value);
  return acc;
}, []);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionagusgambinaView Question on Stackoverflow
Solution 1 - JavascriptxdazzView Answer on Stackoverflow
Solution 2 - JavascriptJonView Answer on Stackoverflow
Solution 3 - JavascriptCedrick CampotoView Answer on Stackoverflow
Solution 4 - JavascriptRobin WieruchView Answer on Stackoverflow