Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

141
Vistas
How can I Neatly format a recursed console-logged Json Object in JavaScript?

I recursively listed a nested json object. When i console log, I get my data in a way that makes it difficult to know where a set of object begins or ends.

How can I format the response?

const obj = {
      fullName: 'Ram Kumar',
      age: 31,
      gender: male
    }
  },
  fullName: 'Ranbir Singh',
    age: 22,
    gender: male
  }
},

function printAllVals(obj) {
  for (let prop in obj) {
    if (typeof obj[prop] === "object") {
      printAllVals(obj[prop])
    } else {
      console.log(prop + ':' + obj[prop]);
    }
  }
}

I am getting:

fullName:Ram Kumar
age: 31
gender: male
fullName:Ranbir Singh
age: 22
gender: male

What I want is something like this. Anything to format or separate the objects for clarity or easy understanding :

fullName:Ram Kumar
age: 31
gender: male 
---
fullName:Ranbir Singh
age: 22
gender: male
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Your object does not look valid, so I added a "child" sub-key to iterate down into.

Before you call the recursive function call, print a line-separator.

const printAllVals = (obj) => {
  for (let prop in obj) {
    if (typeof obj[prop] === 'object') {
      console.log('---');                  // Print line, before the child.
      printAllVals(obj[prop])
    } else {
      console.log(`${prop}: ${obj[prop]}`);
    }
  }
}

const obj = {
  fullName: 'Ram Kumar',
  age: 31,
  gender: 'male',
  child: {                                // sub-key?
    fullName: 'Ranbir Singh',
    age: 22,
    gender: 'male'
  }
};

printAllVals(obj);
.as-console-wrapper { top: 0; max-height: 100% !important; }

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda