Node.js - console.log does not show items in array but instead shows [Object]

后端 未结 4 1511
死守一世寂寞
死守一世寂寞 2020-12-11 23:41

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\', \'ite         


        
4条回答
  •  误落风尘
    2020-12-12 00:13

    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

提交回复
热议问题