Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

117
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!