Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

138
Visualizações
How to perform operations on nested array of objects inside an array using javascript?

I am trying to calculate the average on nested array objects fields which is inside an another array. Here is the array I defined:

    let arr = [
[{"category":"behavioural", "rating":3}, {"category":"technical", "rating":4.5}],
[{"category":"behavioural", "rating":1}, {"category":"technical", "rating":2.5}],
[{"category":"behavioural", "rating":4}, {"category":"technical", "rating":2}]
]

I want to calculate the average of ratings in its respective category and store it in an object.

Expected output:

"metricsaverage" : {
    "behavioral" : 2.66,
    "technical" : 3
}

Where 2.66 and 3 is the average of all ratings with its respective category from the nested array of objects.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Working Demo :

// Input array
let arr = [
[{"category":"behavioural", "rating":3}, {"category":"technical", "rating":4.5}],
[{"category":"behavioural", "rating":1}, {"category":"technical", "rating":2.5}],
[{"category":"behavioural", "rating":4}, {"category":"technical", "rating":2}]
];

// Converting multi-dimensional array into single dimension.
let singleDimensionArr = [].concat(...arr);

// grouping by category and resulting with an object using Array.reduce() method
const groupByCategory = singleDimensionArr.reduce((group, item) => {
  const { category } = item;
  group[category] = group[category] ?? [];
  group[category].push(item.rating);
  return group;
}, {});

// Finally calculating the average based on the category array we have.
Object.keys(groupByCategory).forEach((item) => {
    const len = groupByCategory[item].length;
    groupByCategory[item] = groupByCategory[item].reduce((a, b) => a + b)/len;
})

// result
console.log(groupByCategory);

about 4 years ago · Juan Pablo Isaza Relatório

0

working example here: https://jsfiddle.net/galeroy/ot1pkvqg/4/

  let arr = [
      [{"category":"behavioural", "rating":3}, {"category":"technical", "rating":4.5}],
      [{"category":"behavioural", "rating":1}, {"category":"technical", "rating":2.5}],
      [{"category":"behavioural", "rating":4}, {"category":"technical", "rating":2}]
    ]

    var behavioralTotal = 0,
        behavioralItems = 0,
        technicalTotal = 0,
        technicalItems = 0;

    arr.forEach(function(outerItem){         
      outerItem.forEach(function(innerItem){
        if(innerItem.category === 'behavioural'){
          behavioralTotal += innerItem.rating;
          behavioralItems++;
        }
        else if (innerItem.category === 'technical')
        {
          technicalTotal += innerItem.rating;
          technicalItems++
        }
      })
    });

    console.log(
      'metricsaverage = { \n' +
        '"behavioral" : ' + (behavioralTotal/behavioralItems).toFixed(2) + ',\n' +
        '"technical" : ' + (technicalTotal/technicalItems).toFixed(2) + '\n' +
      '}'
    )
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda