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

155
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 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