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

328
Visualizações
Looping through an object without using Object.entries() on each key of the inner object

I would like to know how to loop through a nested object without specifying keys. I mean just to take the name of the object to do the loop. Is there any method to achieve it? You can see at the bottom what I tried. It actually works, but I want to know if there is a way to do this without mentioning the keys(hero, monster)

Object to loop through:

const characterData = {
  hero: {
    name: "Wizard",
    avatar: "images/wizard.png",
    health: 60,
    diceCount: 3,
    currentDiceScore: []
  },
  monster: {
    name: "Orc",
    avatar: "images/orc.png",
    health: 10,
    diceCount: 1,
    currentDiceScore: []
  },
};

Object.entries(characterData.hero).forEach(([key, value]) => {
  console.log(`${key}: ${value}`)
});

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can first iterate over Object.values() which, in this case, are the nested objects hero, monster and so on.. using Array.prototype.forEach(), and then your code that iterates over Object.entries() again using Array.prototype.forEach()

  • But notice that in the callbackFn function is used the square brackets Destructuring assignment and inside the console.log() Template literals (Template strings):

const characterData = {hero: {name: "Wizard",avatar: "images/wizard.png",health: 60,diceCount: 3,currentDiceScore: []},monster: {name: "Orc",avatar: "images/orc.png",health: 10,diceCount: 1,currentDiceScore: []},}

Object.values(characterData)
  .forEach(o => 
    Object.entries(o)
      .forEach(([k, v]) => 
        console.log(`${k}: ${v}`)))

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