Skip to content
Advertisement

Javascript get the max value grouped by another attribute

Hello I have an array that looks like this

JavaScript

I wanted to get the maximum score for each “zs_numero” and then multiply all the maximum scores with the coefficient to get a final result So i want to get the values like this:

JavaScript

and then make a a variable that calculate all the (zs_coefficient*zs_score) and add them together

I tried this but it didnt work

JavaScript

Advertisement

Answer

You can use Array.reduce() to get the desired result, creating a map keyed on zs_numero. If there is no object present at the zs_numero property or the zs_score is greater than the existing value, we replace it.

We can use Array.reduce() again to calculate the total (sum of zs_coefficient x zs_score);

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