Condense arrays with reduce

Hey Campers!

I need help to understand this code:

var singleVal = array.reduce(function(previousVal, currentVal) {
  return previousVal - currentVal;
}, 0);

I’ve been trying to figure out what the ( 0 zero in the last line) does?

It is the initial value. So I think it would be the “previousVal” the first time the callback function is called.

I cleaned up your code.
You need to use triple backticks to post code to the forum.
See this post for details.

1 Like

var singleVal = array.reduce(function(array,singleVal){
return array+singleVal;
},0);

here “0” is the initial val if we dnt intitlize first elemt will the inital Value :slight_smile:

1 Like

From MDN - Array.prototype.reduce():

arr.reduce(callback[, initialValue])

initialValue:
[Optional] Value to use as the first argument to the first call of the callback. If no initial value is supplied, the first element in the array will be used. Calling reduce on an empty array without an initial value is an error.

Sorry fCC but I didn’t understand this lesson AT ALL. I don’t know what is the usage of this reduce, I cannot understand your example, and that’s it. I think the explanation is not clear at all. It should be rewritten. If someone has a clear description of this, please share it with me.

1 Like

Have you read the mdn docs? https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce

Yes MDN makes me more confused! MDN language is not easy at all.

MDN is the bible but sometimes explanations may be a little too technical for somebody new to fully grasp. For me it is easiest to understand that Reduce is used when you have an array, you want to loop through each element in the array and return some sort of single value (number, object, etc). If you have an array of dog names with their ages, using reduce you can easily calculate what is the grand total age of all dogs.

In additional of MDN, there are multiple blog posts trying to explain the most common and powerful array methods (filter, map, reduce), check those for real examples:
https://hackernoon.com/understanding-map-filter-and-reduce-in-javascript-5df1c7eee464

2 Likes

Thanks for the explanation. I always learn by example! I’ll have a look into them.

I studied it carefully and helped me much! Thank you, now I understand. Do you know what was the problem with fCC’s explanation? The problem is fCC doesn’t imply that those arguments inside the function are kind of “predefined”. That is, they are predefined arguments/values (not sure which one) of those 3 methods (map, reduce, filter). That was the confusing point for me. I don’t know if I managed to discuss it with you properly.

Yeah I think it make sense. FCC is a great tool and resource to give you a path towards understanding a lot of what it takes to build stuff for the web. It is important to understand that FCC is not the only resource out there and does not claim to be the complete guide to everything you need to know. If you get confused about FCC explanation, always look for some help on the forum, research and google examples and other explanations. It will help every time! Best of luck!

2 Likes