5

I have a problem with logging out the contents of an array inside an object. The actual object looks like this

   var stuff = { accepted: [ 'item1', 'item2' ],
         rejected: [],
         response: 'Foo',
         envelope: { from: 'The sender', to: ['new item1', 'new item2'] },
         messageId: 'xxxxxxxxxxxxx' } }

The console.log shows the items of the first array fine but the second array is being output as [Object].

 { accepted: [ 'item1', 'item2' ],
             rejected: [],
             response: 'Foo',
             envelope: { from: 'The sender', to: [Object] },
             messageId: 'xxxxxxxxxxxxx' } } 

What is happening here and how can I get the items of the second array to show when I console.log. Thanks for any help!

UPDATE

Sorry, I forgot to add that I am working exclusively in Node.js so it's a server side log that needs to display the object exactly as it's received from a callback with a straight console.log, ie. no further stringify-ing.

I also just tried creating another random object with a similar structure like this.

 var objText = {
      fun: {
        stuff: 'Some stuff',
        list: ['this', 'it', 'takes']
      }
    };

The console.log for the above is:

{ fun: { stuff: 'Some stuff', list: [ 'this', 'it', 'takes' ] } }

This appears to be the same structure to me and yet the console.log works fine so it seems to be perfectly possible in Node to log arrays content even when it's embedded inside and an object inside an outer object.

mikeym
  • 5,705
  • 8
  • 42
  • 62

4 Answers4

8

It looks like this is an old topic, anyway

I've faced the same issue, embedded array printed as [Array].

It is because of console.log in the node.js uses util.inspect for print, the default depth is 2. So, to print something which is deeper than 2 followings can be used:

const util = require('util')
console.log(util.inspect(errors, true, 10))
Dzmitry Atkayey
  • 332
  • 5
  • 12
  • 1
    This should be the accepted answer: 1. `console.log` does use `util.inspect` with depth = 2 (hence the problem of OP), and 2. using the suggested solution does remedy the issue. Also, if you add last parameter to `util.inspect` and pass `true`, `console.log` will behave the same as before (e.g. show colours as well). Depth (here 10) can be adjusted accordingly. – Justin Case May 04 '21 at 10:24
7

This is the default way for some browser and implementations of showing too complex or deep objects/arrays with console.log. An alternative is to use JSON.stringify with console.log:

var stuff = {
  accepted: ['item1', 'item2'],
  rejected: [],
  response: 'Foo',
  envelope: {
    from: 'The sender',
    to: ['new item1', 'new item2']
  },
  messageId: 'xxxxxxxxxxxxx'
}


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

EDIT:

Another alternative is to use console.dir in case you have a too complex or recursive object, see https://stackoverflow.com/a/27534731/6051261

Daniel Pérez
  • 1,873
  • 1
  • 17
  • 25
3

Try it with: console.log(JSON.stringify(variable))

g0xr
  • 46
  • 5
0

If you like Chrome devtools, that folds your json objects and let you observe a lot of things, you can use the --inspect flag:

node --inspect index.js

The console will then give you an URL and you just have to copy paste in Google Chrome to enjoy Google Chrome console.

More information on this link

Hammerbot
  • 15,696
  • 9
  • 61
  • 103