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

121
Visualizações
How to find sum of all properties of the object and save it as another property in the same object

I need to find sum of all property values and save it into another (totalScore) property of the same object

What I have tried:

Below function is tested and returns sum of all object properties except totalScore (or k = any property) property.

const calculateScore = (my_object) => {
  const allowed = Object.keys(my_object).reduce((obj, k) => {
    if (k != 'totalScore') obj[k] = my_object[k];
    return obj;
  }, {});

  return Object.values(allowed).reduce((a, b) => a + b);
}

Let's say I have this object:

const obje = {
  prop1: 1,
  prop2: 2,
  prop3: 3,
  totalScore: calculateScore(this)
}

I tried just invoking calculateScore function above and received a Function as totalScore, moreover this keyword is pointing to a Global object.

I also tried IIFE, so totalScore runs whenever Object (and its constructor) is created:

const obje = {
  prop1: 1,
  prop2: 2,
  prop3: 3,
  totalScore: (() => calculateScore(this))()
}

But still no success. I would like this way to work and or would be glad to see a better implementation of my use case.

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