Skip to content
Advertisement

Javascript get the max value grouped by another attribute

Hello I have an array that looks like this

result.entities= [
  {zs_numero: "1", "zs_coefficient": "2", "zs_score": "50"},
  {zs_numero: "1", "zs_coefficient": "2", "zs_score": "55"},
  {zs_numero: "2", "zs_coefficient": "4", "zs_score": "2"},
  {zs_numero: "2", "zs_coefficient": "4", "zs_score": "10"},
  {zs_numero: "3", "zs_coefficient": "3", "zs_score": "33"},
  {zs_numero: "3", "zs_coefficient": "3", "zs_score": "35"},
]

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:

newarray= [
  {zs_numero: "1", "zs_coefficient": "2", "zs_score": "55"},
  {zs_numero: "2", "zs_coefficient": "4", "zs_score": "10"},
  {zs_numero: "3", "zs_coefficient": "3", "zs_score": "35"},
]

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

        var newarray = []
        result.entities.forEach(function (a) {
            if (!this[a.zs_numero]) {
                this[a.zs_numero] = { zs_numero: a.zs_numero, zs_coefficient: 0, zs_score: 0 };
                newarray.push(this[a.zs_numero]);
            }
            this[a.zs_numero].zs_coefficient = a.zs_coefficient;
            this[a.zs_numero].zs_score = Math.max(this[a.zs_numero].zs_score, a.zs_score);
        }, Object.create(null));
        console.log(newarray)

        for(var i = 0; i < newarray.length; i++){
          max=max+newarray.zs_coefficient*newarray.zs_score
        }

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);

result = { 
  entities: [
    {zs_numero: "1", "zs_coefficient": "2", "zs_score": "50"},
    {zs_numero: "1", "zs_coefficient": "2", "zs_score": "55"},
    {zs_numero: "2", "zs_coefficient": "4", "zs_score": "2"},
    {zs_numero: "2", "zs_coefficient": "4", "zs_score": "10"},
    {zs_numero: "3", "zs_coefficient": "3", "zs_score": "33"},
    {zs_numero: "3", "zs_coefficient": "3", "zs_score": "35"},
  ]
}

const output = Object.values(result.entities.reduce ((acc, cur) => { 
    if (!acc[cur.zs_numero] || (+cur.zs_score > +acc[cur.zs_numero].zs_score)) {
        acc[cur.zs_numero] = cur;
    }
    return acc;
}, {}));

console.log('Output:', output)

const total = output.reduce((acc, cur) => { 
    return acc + cur.zs_score * cur.zs_coefficient
}, 0);
console.log('Total score:', total);
.as-console-wrapper { max-height: 100% !important; top: 0; }
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement