Skip to content
Advertisement

Javascript reduce() on Object

There is nice Array method reduce() to get one value from the Array. Example:

JavaScript

What is the best way to achieve the same with objects? I’d like to do this:

JavaScript

However, Object does not seem to have any reduce() method implemented.

Advertisement

Answer

What you actually want in this case are the Object.values. Here is a concise ES6 implementation with that in mind:

JavaScript

or simply:

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement