2

When I have a lot of objects together and I run it using nodejs in cmd It says the [Object] instead of the actual contents of the object.

here is an example from my command prompt example of problem

myObject = {
  '698045139763069009':{ users: { '560433156377804828': {name: "mark", age: "28"}},info: {} }
}
console.log(myObject);

code here ^^^

same thing happens with an array

myObject = {
  '698045139763069009':{ users: { '560433156377804828': ["mark", 28]},info: {} }
}
console.log(myObject);

example

and a screen shot ^^^

help would be appreciated! thanks

Mikitaka
  • 49
  • 1
  • 6

4 Answers4

3

Looks like your console doesn't print nested objects.

Change console.log(someObject) to console.log(JSON.stringify(someObject)).

Note that it will give error if the object is cyclic / has circular references.

chiragrtr
  • 902
  • 4
  • 6
  • Makes sense, because I can still retrieve and put in data. I can also do `console.log(myObject.users)` and see that all the data is still there – Mikitaka May 14 '20 at 03:45
  • `console.log(myObject.users)` may not print complete `users` object if it has nesting as well so better to do `stringify`. – chiragrtr May 14 '20 at 03:48
2

It's to make the output more readable.

If you want to print the whole object use util.inspect() as described here or use JSON.stringify().

Marco
  • 7,007
  • 2
  • 19
  • 49
0

Turns out my console doesn't print nested arrays or objects. Thanks for all the help!

It's to make it more readable when working with large amounts of nested arrays and objects.

Mikitaka
  • 49
  • 1
  • 6
0

I think the answer to this (and which uses util.inspect()), that will also display nested objects, is console.dir().

Consider below example:

  {
    fieldName: 'price_point',
    value: [ 'priced at 25 dollars' ],
    entityValue: [ [Object] ] <----- (this annoying thing here)
  }

TURNS THIS INTO

{
  fieldName: 'price_point',
  value: [ 'priced at 25 dollars' ],
  entityValue: [ { number: 25, units: 'Dollar' } ] <--- (to this!)
}

JSON.stringify() DOES NOT show nested arrays and objects.

Gel
  • 2,866
  • 2
  • 18
  • 25