我有这样一个对象:

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的第三个参数定义了空格插入 打印格式。它可以是字符串或数字(空格数)。

其他回答

const myObject = {
   "a":"a",
   "b":{
      "c":"c",
      "d":{
         "e":"e",
         "f":{
            "g":"g",
            "h":{
               "i":"i"
            }
         }
      }
   }
};
console.log(JSON.stringify(myObject));

输出:

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

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

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

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

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

您可以使用JSON。Stringify,并获得一些漂亮的缩进,以及可能更容易记住语法。

console.log(JSON.stringify(myObject, null, 4));

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

第三个参数设置缩进级别,因此您可以根据需要进行调整。

如果需要,请在JSON stringify MDN文档中查看更多详细信息。

这两种用法都可以应用:

// 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.js 6.4.0开始,这个问题可以用util.inspect.defaultOptions优雅地解决:

require("util").inspect.defaultOptions.depth = null;
console.log(myObject);