Binuriki Cliean Jay
Binuriki Cliean Jay

Reputation: 98

Javascript How to get the properties of an object property

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

Answers (4)

omkarb
omkarb

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

Ziv Ben-Or
Ziv Ben-Or

Reputation: 1194

  1. Yon need to check typeof blog[key] (the value) and not typeof key (the key).

  2. Yon need to writeblog[key][key2] insted of key[key2].

  3. Better to use: 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

Phil
Phil

Reputation: 164731

Summarising my comments...

  1. Your else condition is never running because key is always a string. You probably meant if (typeof blog[key] !== 'object')
  2. Since 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

Code Maniac
Code Maniac

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

Related Questions