Home > Enterprise >  Javascript get the max value grouped by another attribute
Javascript get the max value grouped by another attribute

Time:10-27

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
        }

CodePudding user response:

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; }
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

Here is a simple loop to solve your problem.

const 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"},
]
let finalResult = []
entities.map(d => {
  let index = finalResult.findIndex(p => p.zs_numero == d.zs_numero)
  if(index == -1) {
    finalResult.push(d)
  } else {
    if(parseInt(d.zs_score) > parseInt(finalResult[index].zs_score)) {
      finalResult[index] = {...d};
    }
  }
})

console.log(finalResult)

let Total = 0;
finalResult.map(d=> {Total  = (d.zs_score * d.zs_coefficient)})
console.log(Total)
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related