Reputation: 98
I am trying to display the properties of an object. one of the properties is an object.
let myBlog = {
title: 'My First Blog',
body: 'This is the first article that blah lorem lorem lorem blah',
author: 'Koko Bleh Bleh',
views: 0,
comments: {
author: 'TheLegend27',
body: 'The legendary commentator'
},
isLive: false
};
function showBlog(blog)
{
for(let key in blog)
{
if( typeof key !== 'object')
{
console.log(key + ': ' + blog[key]);
}
else
{
console.log(key);
for(let key2 in key)
{
console.log(key2 + ': ' + key[key2]);
}
}
}
}
As I was iterating using loop to display the properties and values, the object property named 'comments' is just displaying: 'comment [object object]'
but I want to display it as: 'comment author: value body: some body'
Upvotes: 0
Views: 134
Reputation: 1
We can use this simple code which results like
title : My First Blog
body : This is the first article that blah lorem lorem lorem blah
author : Koko Bleh Bleh
views : 0
comments author : TheLegend27
comments body : The legendary commentator
isLive : false
function displayObjectValues(myObj, parentObjKey = '') {
for (let [key, value] of Object.entries(myObj)) {
if (typeof value == 'object') {
displayObjectValues(value, key);
} else {
console.log(`${parentObjKey} ${key} : ${value}`);
}
}
}
Upvotes: 0
Reputation: 1194
Yon need to check typeof blog[key]
(the value) and not typeof
key
(the key).
Yon need to writeblog[key][key2]
insted of key[key2]
.
blog[key].toString() !== "[object Object]"
because, Date
, Array
and null
are also Object
.var myBlog = {
title: 'My First Blog',
body: 'This is the first article that blah lorem lorem lorem blah',
author: 'Koko Bleh Bleh',
views: 0,
comments: {
author: 'TheLegend27',
body: 'The legendary commentator'
},
isLive: false
};
function showBlog(blog) {
for (let key in blog) {
if (blog[key].toString() !== "[object Object]") {
console.log(key + ': ' + blog[key]);
} else {
console.log(key + ':');
for (let key2 in blog[key]) {
console.log(` ${key2}: ${blog[key][key2]}` );
}
}
}
}
console.log(showBlog(myBlog))
Upvotes: 2
Reputation: 164731
Summarising my comments...
else
condition is never running because key
is always a string. You probably meant if (typeof blog[key] !== 'object')
key
is a string, for (let key2 in key)
and key[key2]
won't be what you want. You probably meant blog[key]
. But how about using recursion... else { showBlog(blog[key]) }
let myBlog = {
title: 'My First Blog',
body: 'This is the first article that blah lorem lorem lorem blah',
author: 'Koko Bleh Bleh',
views: 0,
comments: {
author: 'TheLegend27',
body: 'The legendary commentator'
},
isLive: false
};
function showBlog(blog)
{
for(let key in blog)
{
if( typeof blog[key] !== 'object')
{
console.log(key + ': ' + blog[key]);
}
else
{
console.log(key);
showBlog(blog[key]) // recursively call
}
}
}
showBlog(myBlog)
Upvotes: 1
Reputation: 37757
You're coercing object to string here
console.log(key + ': ' + blog[key]);
which is why it is printing comment [object object]
let myBlog = {
title: 'My First Blog',
body: 'This is the first article that blah lorem lorem lorem blah',
author: 'Koko Bleh Bleh',
views: 0,
comments: {
author: 'TheLegend27',
body: 'The legendary commentator'
},
isLive: false
};
function showBlog(blog) {
for (let key in blog) {
if (typeof key !== 'object') {
console.log(key + ': ', blog[key]);
} else {
console.log(key);
for (let key2 in key) {
console.log(key2 + ': ', key[key2]);
}
}
}
}
showBlog(myBlog)
Upvotes: 1