Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

113
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda