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

154
Views
How to sum the values in this object of objects in javascript?
const ob = {
    a: 1,
    b: {
        c: 3,
        d: 6,
        e: {
            f: {
                g: 3,
                h: {
                    i: 5,
                    j: {
                        k: 7
                    }
                }
            }
        }
    }
};

Any methods to solve this code?

I have no idea how to solve this code.

For abovementioned input I would expect a result of 1 + 3 + 6 + 3 + 5 + 7 = 25. So what I want to return from a function sumObject(ob) is: 25

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You can try reduce with recursion

The condition for the sum is

  • If the current value is a number, sum it with result
  • If the current value is not a number (in your case, it's an object), call the function sumObject recursively with current result

const ob = {
  a: 1,
  b: {
    c: 3,
    d: 6,
    e: {
      f: {
        g: 3,
        h: {
          i: 5,
          j: {
            k: 7
          }
        }
      }
    }
  }
};

function sumObject(data, result = 0) {
  return Object.values(data).reduce((sum, value) => typeof value === 'number' ? sum + value : sumObject(value, sum), result)
}

console.log(sumObject(ob))

about 4 years ago · Juan Pablo Isaza Report

0

If you don't understand some of the other answers, this is an easier solution to understand:

function sumObject(obj){
  
  let result = 0;
  
  for(let i of Object.values(obj)){ //we iterate through all values in obj
    
    if(typeof i == "number"){ //if the current value of i is a number
      
      result+=i; //then add that to the result
      
    } else { //otherwise, it will be an object
      
      result+=sumObject(i); //so we call the function on itself to iterate over this new object
      
    }
    
  }
  
  return result; //finally, we return the total
  
}

console.log(sumObject(ob));
about 4 years ago · Juan Pablo Isaza Report

0

const ob = {
    a: 1,
    b: {
        c: 3,
        d: 6,
        e: {
            f: {
                g: 3,
                h: {
                    i: 5,
                    j: {
                        k: 7
                    }
                }
            }
        }
    }
}; 

const sum = data =>
   Object
   .keys(data)
   .reduce((a,b) => a + (typeof(s = data[b]) == "number" ? s : sum(s)), 0);

console.log(sum(ob))

about 4 years ago · Juan Pablo Isaza 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!