Home >Web Front-end >JS Tutorial >How Can I Achieve Full Object Display in Node.js's console.log()?

How Can I Achieve Full Object Display in Node.js's console.log()?

DDD
DDDOriginal
2024-12-14 10:34:12541browse

How Can I Achieve Full Object Display in Node.js's console.log()?

Full Object Display in Node.js's Console.log()

When working with objects in Node.js, it can be frustrating to receive only a partial representation with console.log(). This representation displays nested objects as "[Object]" rather than their actual contents.

Consider the object below:

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

Console.log(myObject) would output:

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

To display the full object, including nested contents, utilize the util.inspect() method:

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

This will output the complete object, including the contents of property f:

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

The above is the detailed content of How Can I Achieve Full Object Display in Node.js's console.log()?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn