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

322
Visualizações
Sum values of a JavaScript object

Given an Object of this structure

{
    "1": {
        "data": {
            "a": 100,
            "z": 100,
            "e": 50
        },
        "percentage": {
            "a": 10,
            "z": 10,
            "e": 41.09
        }
    },
    "2": {
        "data": {
            "a": 10,
            "z": 10,
            "e": 20
        },
        "percentage": {
            "a": 30,
            "z": 40,
            "e": 50
        }
    }
}

i want to sum the values of this object in order to get this similar structure

{
    "data": {
        "a": sum(a),
        "z": sum(z),
        "e": sum(e)
    },
    "percentage": {
        "a": sum(a)/numberOfelements,
        "z": sum(z)/numberOfelements,
        "e": sum(e)/numberOfelements
    }
}

As am just started learnig js i find it hard to implement the sum inside this map

Object.keys(filtered).map(key => ({
        data: data[key].data,
        percentage: data[key].percentage,
      }))

any help would be appriciated

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

0

If numOfElemements meant elements in data object, then current implementation will do. Else uncomment the existing commented lines, and comment the line after the last commented line. Which considers elements in the data object.

const obj = {
  1: {
    data: {
      a: 100,
      z: 100,
      e: 50,
    },
  },
  2: {
    data: {
      a: 10,
      z: 10,
      e: 20,
    },
  },
};
function groupBySum(obj) {
  let data = {};
  let percentage = {};
  // let numberOfElements = 0;
  for (let i in obj) {
    // numberOfElements += 1;
    for (let j in obj[i].data) {
      if (data[j] === undefined) {
        data[j] = obj[i].data[j];
      } else {
        data[j] += obj[i].data[j];
      }
    }
  }
  for (let i in data) {
    // percentage[i] = data[i] / numberOfElements;
    percentage[i] = data[i] / Object.keys(data).length;
  }
  return {
    data,
    percentage,
  };
}
console.log(groupBySum(obj));

Also it'll be a good idea to change your current structure to array format as below.

const obj = [
  {
    data: {
      a: 100,
      z: 100,
      e: 50,
    },
    percentage: {
      a: 10,
      z: 10,
      e: 41.09,
    },
  },
  {
    data: {
      a: 10,
      z: 10,
      e: 20,
    },
    percentage: {
      a: 30,
      z: 40,
      e: 50,
    },
  },
];
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