我有这样一个对象:

const myObject = {
   "a":"a",
   "b":{
      "c":"c",
      "d":{
         "e":"e",
         "f":{
            "g":"g",
            "h":{
               "i":"i"
            }
         }
      }
   }
};

但是当我尝试使用console.log(myObject)显示它时,我收到这样的输出:

{ a: 'a', b: { c: 'c', d: { e: 'e', f: [Object] } } }

我怎样才能得到完整的对象,包括属性f的内容?


当前回答

节点REPL有一个内置的解决方案来覆盖对象的显示方式,请参见这里。

REPL模块内部在打印值时使用util.inspect()。 然而,实效。Inspect将调用委托给对象的Inspect () 函数,如果有的话。

其他回答

这两种用法都可以应用:

// more compact, and colour can be applied (better for process managers logging)
console.dir(queryArgs, { depth: null, colors: true });

// get a clear list of actual values
console.log(JSON.stringify(queryArgs, undefined, 2));

也许控制台。Dir就是你所需要的。

http://nodejs.org/api/console.html#console_console_dir_obj

使用实效。检查obj并将结果字符串打印到标准输出。

如果您需要更多的控制,请使用util选项。

另一个简单的方法是将其转换为json

console.log('connection : %j', myObject);

一个简单的技巧是在运行脚本时使用调试模块添加DEBUG_DEPTH=null作为环境变量

Ex.

DEBUG=* DEBUG_DEPTH=null节点index.js

在代码中

const debug = require('debug');
debug("%O", myObject);

简单的选择:

console.log ' % ', myObject);