最近的节点console.log(object)
回报版本:如何使node.js记录格式关闭parens以匹配开始的parens?
{ top:
{ child:
{ anotherChild: 'someValue' } } }
我如何让它回到一个更传统的:
{
top: {
child: {
anotherChild: 'someValue'
}
}
}
谢谢!
最近的节点console.log(object)
回报版本:如何使node.js记录格式关闭parens以匹配开始的parens?
{ top:
{ child:
{ anotherChild: 'someValue' } } }
我如何让它回到一个更传统的:
{
top: {
child: {
anotherChild: 'someValue'
}
}
}
谢谢!
这是可能的我误解了这个问题......是否有一个原因,你不能使用JSON.strinfigy
格式化对象,然后console.log
它? JSON.stringify
接受控制缩进级别的附加参数。
> a = { top: { child: { anotherChild: 'someValue' } } }
> console.log(JSON.stringify(a, null, 2))
{
"top": {
"child": {
"anotherChild": "someValue"
}
}
}
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify
JSON.stringify(值[,替换器[,空间]])
特别
空间可选
一个字符串或Number对象t用于为输出可读性目的在输出JSON字符串中插入空格。如果这是一个数字, 它表示要用作空格的空格字符数; 如果这个数字大于这个数字,则这个数字的上限为10。小于 的值表示不应使用空格。如果这是一个字符串,则 字符串(或字符串的前10个字符,如果它比 长)用作空格。如果未提供此参数(或者是 null),则不使用空白。
是的。如果可能的话,我想更改控制台日志的行为,而不必制作调用console.log的别名。 – mikemaccana
你尝试过类似'util.inspect'吗? https://nodejs.org/api/util.html –