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

108
Visualizações
calculating average of nested array of objects' attributes - Universal

I want to calculate the average of an array consisting of nested objects. Now the problem is that what I have now is extremely hardcoded but also not dependent on the depth of the array. Imagine the exampleArray also got two new keys under the each course called "midterm" and "fullterm" forexample where they all consisted of age 15-17 than my code will stop working. I am pulling from a API that has different depths on the nested objects. Is there a way to make this work not depending on hardcoding like it is now?

const courses = ["math", "english", "geography"]

const exampleArray = [{
    name: "Tim",
    class: "C",
    value: { //key is age and key is percentage on test he had at that age
      math: {
        "15": 46,
        "16": 49,
        "17": 97
      },
      english: {
        "15": 21,
        "16": 20,
        "17": 0
      },
      geography: {
        "15": 96,
        "16": 31,
        "17": 88
      }
    }
  },
  {
    name: "Jack",
    class: "C",
    value: { //key is age and key is percentage on test he had at that age
      math: {
        "15": 49,
        "16": 15,
        "17": 96
      },
      english: {
        "15": 21,
        "16": 20,
        "17": 56
      },
      geography: {
        "15": 0,
        "16": 38,
        "17": 77
      }
    }
  },
  {
    name: "Catherine",
    class: "C",
    value: {
      math: { //key is age and key is percentage on test he had at that age
        "15": 44,
        "16": 19,
        "17": 93
      },
      english: {
        "15": 0,
        "16": 28,
        "17": 59
      },
      geography: {
        "15": 14,
        "16": 18,
        "17": 27
      }
    }
  }
]

const courseArray = Object.keys(exampleArray[0].value)
const course = [courseArray[0]]
const ageArray = Object.keys(exampleArray[0].value[course]);

function CalcAvg(course, ageArray) {
  const emptyObject = {}
  ageArray.map((age) => {
    const val =(exampleArray.map(function(obj) {
          return obj.value[course][age]
        })
        .reduce(function(a, b) {
          return a + b
        }) / exampleArray.length)
    emptyObject[age] = val
  })
  return emptyObject
}
const testObject = {Class:"C",value:{}}

courseArray.map((course) => {
  testObject["value"][course] = CalcAvg(course, ageArray)
})
console.log(testObject)
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