I'm trying to go through each key in an object and print it out as if JSON.stringify would output it as
to look like
{
"name": "james",
"profile": {
"location": "ireland",
"address": {
"city": "dublin"
},
"hobbies": {}
}
}
so far it's outputting as
"name": "James",
"profile": {
"location": "ireland",
"address": {
"city": "dublin",
}
const output = (data, node) => {
for (let key in data) {
if (typeof data[key] === 'object' && Object.keys(data[key]).length > 0) {
if (node === true) {
console.log(` "${key}": {`)
} else {
console.log(`"${key}": {`)
}
output(data[key], true)
} else {
console.log(`"${key}": "${Object.keys(data[key]).length > 0 ? data[key] : '{}' }",`);
}
}
console.log(`}`)
}
const obj = {
"name": "James",
"profile": {
"location": "ireland",
"address": {
"city": "dublin"
},
"hobbies": {}
}
};
output(obj);
how should I output the tabs properly for each key inside of an object?
from output javascript object in specific style
No comments:
Post a Comment