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

139
Views
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 answers
Answer question

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