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

161
Views
How to sum two object values in javascript

I'm stuck how i sum two object like this:

obj1 = { 
  'over_due_data': 10,
  'text_data': 5
} 

obj2 = {
  'over_due_data': 20,
  'text_data': 5
}

I went this output

obj = {
  'over_due_data': 30,
  'text_data': 10
}

One more thing, don't use a for loop, merge and extend. Is it possible to sum two objects?

about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

try with simply use Object.keys() and Array#map() function

obj1 = {
  'over_due_data': 10,
  'text_data': 5
}
obj2 = {
  'over_due_data': 20,
  'text_data': 5
}
var obj ={}
Object.keys(obj1).forEach(function(a){
  obj[a] = obj1[a] +obj2[a]

})
console.log(obj)

about 4 years ago · Santiago Trujillo Report

0

Another possible solution, using Array#reduce.

var obj1 = {'over_due_data':10,'text_data':5}, obj2 = {'over_due_data':20,'text_data':5},
    obj = Object.keys(obj1).reduce(function(s,a) {
      s[a] = obj1[a] + obj2[a];
      return s;
    }, {})
    console.log(obj);

about 4 years ago · Santiago Trujillo Report

0

Try something like this if you don't want to use loop

    obj1 = { 
      'over_due_data': 10,
      'text_data': 5
    } 
    
    obj2 = {
      'over_due_data': 20,
      'text_data': 5
    }
    var obj = {};
    obj['over_due_data'] = obj1['over_due_data'] + obj2['over_due_data']
    obj['text_data'] = obj1['text_data'] + obj2['text_data']

    console.log(obj)

about 4 years ago · Santiago Trujillo Report
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!