The trick to making console.log play nice with complex objects

console.log is useful in a lot of situations (although there are usually better ways to debug if that’s what you’re doing).

In the browser console.log works well with objects, you can drill down as much as you need. But in Node.js, when you look at th…


This content originally appeared on DEV Community and was authored by Paul Oms

console.log is useful in a lot of situations (although there are usually better ways to debug if that's what you're doing).

In the browser console.log works well with objects, you can drill down as much as you need. But in Node.js, when you look at the output of a nested object, you'll often see this:

$ console.log({a: {b: { c: {d: {}}}}})

{ a: { b: { c: [Object] } } }

d: {} was replaced with [Object]. But why?

It's because the command line / terminal doesn't have a nice UI for drilling down, so Node attempts to print up to three levels deep. Beyond three levels it just prints [Object].

This is controlled by a variable in the node 'util' module, called depth, which defaults to 2. You can set it yourself here:

require('util').inspect.defaultOptions.depth = 0; // top level only, e.g.:
// { a: [Object] }
require('util').inspect.defaultOptions.depth = null; // print everything, e.g.: 
// {
//  a: { b: { c: { d: {} } } }
// }

It's not a great idea to change an underlying variable, as it might come back to bite later. So a cleaner way is to convert the JSON object to a string and log that. We can use node's built in JSON class and the stringify method:

complexObject = {a: {b: { c: {d: {}}}}}
console.log(JSON.stringify(complexObject, null, 2))

// {
//   "a": {
//     "b": {
//       "c": {
//         "d": {}
//       }
//     }
//   }
// }

Note that the 3rd parameter of JSON.stringify, Number 2, controls the number of spaces of indentation, and the 2nd parameter can be used to filter or adjust the objects and properties shown.

Now you can really see what's in those deep Objects.


This content originally appeared on DEV Community and was authored by Paul Oms


Print Share Comment Cite Upload Translate Updates
APA

Paul Oms | Sciencx (2021-10-21T20:03:24+00:00) The trick to making console.log play nice with complex objects. Retrieved from https://www.scien.cx/2021/10/21/the-trick-to-making-console-log-play-nice-with-complex-objects/

MLA
" » The trick to making console.log play nice with complex objects." Paul Oms | Sciencx - Thursday October 21, 2021, https://www.scien.cx/2021/10/21/the-trick-to-making-console-log-play-nice-with-complex-objects/
HARVARD
Paul Oms | Sciencx Thursday October 21, 2021 » The trick to making console.log play nice with complex objects., viewed ,<https://www.scien.cx/2021/10/21/the-trick-to-making-console-log-play-nice-with-complex-objects/>
VANCOUVER
Paul Oms | Sciencx - » The trick to making console.log play nice with complex objects. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/10/21/the-trick-to-making-console-log-play-nice-with-complex-objects/
CHICAGO
" » The trick to making console.log play nice with complex objects." Paul Oms | Sciencx - Accessed . https://www.scien.cx/2021/10/21/the-trick-to-making-console-log-play-nice-with-complex-objects/
IEEE
" » The trick to making console.log play nice with complex objects." Paul Oms | Sciencx [Online]. Available: https://www.scien.cx/2021/10/21/the-trick-to-making-console-log-play-nice-with-complex-objects/. [Accessed: ]
rf:citation
» The trick to making console.log play nice with complex objects | Paul Oms | Sciencx | https://www.scien.cx/2021/10/21/the-trick-to-making-console-log-play-nice-with-complex-objects/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.