AntonNiklasson
AntonNiklasson

Reputation: 1749

How do I access nested objects properties in console.table()?

Lets say I have the following array of objects:

var data = [
  { id: 123, author: { id: 123 } },
  { id: 123, author: { id: 123 } }
];

How can I populate a column in console.table with the id property of the author object?

This does not seem to work: console.table(data, ['id', 'author.id']);

Upvotes: 5

Views: 5233

Answers (1)

Andy
Andy

Reputation: 63550

I'm not sure you can do it with nested properties.

You could use map to pull the data out into a better format and then console.table it:

const data = [
  { id: 123, author: { id: 123 } },
  { id: 123, author: { id: 123 } }
];

const out = data.map(obj => {
  return {
    id: obj.id,
    authorId: obj.author.id
  };
});

console.table(out);

Note: you cannot hide the index column.

Upvotes: 7

Related Questions