Skip to content
Advertisement

In a javascript object, how to sum values from one field if a tag exists in another field?

If the data is like: –

JavaScript

And I want to create another json array which has aggregate damage per unique tag in the Hazard type. What should be the approach here? (Very new to Javascript)

Advertisement

Answer

Here’s one way you might accomplish the task, making use of Array.filter and Array.reduce.

Note that I changed the values of Crop_Damage from zeros to 3 and 1, to make it more evident the code works.

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