简体   繁体   English

如何访问 console.table() 中的嵌套对象属性?

[英]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?如何使用作者 object 的 id 属性填充 console.table 中的列?

This does not seem to work: console.table(data, ['id', 'author.id']);这似乎不起作用: console.table(data, ['id', 'author.id']);

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:您可以使用map将数据提取为更好的格式,然后使用console.table它:

var out = data.map(function (el) {
  return {
    id: el.id,
    authorId: el.author.id
  };
});

console.table(out);

You can use the optional columns parameter to select a subset of columns to display您可以使用可选的列参数来选择要显示的列子集

 var data = [ { id: 123, author: { id: 123 } }, { id: 123, author: { id: 123 } } ]; console.table(data,['id','author.id'])

输出:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM