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

203
Visualizações
How to calculate the average of all object properties without specifying them explicitly by name?

I'm trying to figure out a robust way to calculate the average of object properties in case there are too many to be specified explicitly by their name.

I found this nice gist that helps in case we have just a few properties:

const someData = 
[   
    { height: 176, weight: 87 },
    { height: 190, weight: 103 },
    { height: 180, weight: 98 } 
]

// for height
var sumHeight = (prev, cur) => ({height: prev.height + cur.height});
var avgHeight = someData.reduce(sumHeight).height / someData.length;
console.log(avgHeight); // => gives 182

// for weight
var sumWeight = (prev, cur) => ({weight: prev.weight + cur.weight});
var avgWeight = someData.reduce(sumWeight).weight / someData.length;
console.log(avgWeight); // => gives 96

But this method is limited in terms of scaling if we have lots of properties, for example:

const someDataExtended = 
[   
    { height: 176, weight: 87, salary: 100000, age: 20, numOfCats: 2 },
    { height: 190, weight: 103, salary: 100050, age: 40, numOfCats: 0 },
    { height: 180, weight: 98, salary: 20345, age: 50, numOfCats: 1 } 
]

How can I average across all properties without specifying them by name? Ideally, I'd like to map over someDataExtended without mutating the initial data, but rather generate a summary such as:

const finalSummary = {
    height: 182,
    weight: 96,
    salary: 73465,
    numOfCats: 1
}
about 4 years ago · Juan Pablo Isaza
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