Skip to content
Advertisement

How to recalculate an object from a nested objects?

I have an object where Citems is an array of Object. Each object has status on or of and time.

JavaScript

I want to generate an array or object from it that show total time for each status like below

JavaScript

I tried with map and reduce but getting confused.

Advertisement

Answer

You just need a nested ‘sum’, here implemented using reduce() and making use of computed properties to update the accumulator using the status as key.

JavaScript

Or using a for...of loop

JavaScript

To extend this to an array of such Chapter objects you could nest it once more in a reduce().

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