我有这样一个对象:

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的内容?


当前回答

我觉得这可能对你有用。

const myObject = { “一”:“一”, " b ": { “c”:“c”, " d ": { “e”:“e”, " f ": { “g”:“g”, " h ": { “我”:“我” } } } } }; console.log (JSON。stringify(myObject, null, '\t'));

如本回答所述:

JSON。Stringify的第三个参数定义了空格插入 打印格式。它可以是字符串或数字(空格数)。

其他回答

如果你正在寻找一种方法来显示数组中隐藏的项,你必须传递maxArrayLength: Infinity

console.log(util.inspect(value, { maxArrayLength: Infinity }));

你需要使用util.inspect():

const util = require('util')

console.log(util.inspect(myObject, {showHidden: false, depth: null, colors: true}))

// alternative shortcut
console.log(util.inspect(myObject, false, null, true /* enable colors */))

输出

{ a: 'a',  b: { c: 'c', d: { e: 'e', f: { g: 'g', h: { i: 'i' } } } } }

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

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

这两种用法都可以应用:

// 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));

检查对象的一个好方法是使用node -inspect选项与Chrome DevTools for node。

node.exe --inspect www.js

打开chrome://inspect/#设备,单击Open dedicated DevTools for Node

现在每个被记录的对象都可以在检查器中使用,就像在chrome中运行的常规JS一样。

不需要重新打开检查器,只要节点启动或重启,它就自动连接到节点。——inspect和Chrome DevTools for Node可能在旧版本的Node和Chrome中不可用。