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
Release: 2024-12-14 10:34:12
Original
547 people have browsed it

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"
            }
         }
      }
   }
};
Copy after login

Console.log(myObject) would output:

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

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 */))
Copy after login

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' } } } } }
Copy after login

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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template