简体   繁体   English

当 object 在多个其他对象中时,console.log 返回 [Object]

[英]console.log returns [Object] when object is in multiple other objects

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.当我有很多对象并在 cmd 中使用 nodejs 运行它时,它显示的是 [Object] 而不是 object 的实际内容。

here is an example from my command prompt这是我的命令提示符中的一个示例问题的例子

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);

例子

and a screen shot ^^^和截图^^^

help would be appreciated!帮助将不胜感激! thanks谢谢

It's to make the output more readable.这是为了让 output 更具可读性。

If you want to print the whole object use util.inspect() as described here or use JSON.stringify() .如果要打印整个 object 使用util.inspect() ,如此处所述或使用JSON.stringify()

Looks like your console doesn't print nested objects.看起来您的控制台不打印嵌套对象。

Change console.log(someObject) to console.log(JSON.stringify(someObject)) .console.log(someObject)更改为console.log(JSON.stringify(someObject))

Note that it will give error if the object is cyclic / has circular references.请注意,如果 object 是循环的/具有循环引用,则会出错。

Turns out my console doesn't print nested arrays or objects.结果我的控制台没有打印嵌套的 arrays 或对象。 Thanks for all the help!感谢所有的帮助!

It's to make it more readable when working with large amounts of nested arrays and objects.这是为了在处理大量嵌套的 arrays 和对象时使其更具可读性。

I think the answer to this (and which uses util.inspect() ), that will also display nested objects, is console.dir() .我认为这个也将显示嵌套对象的答案(并且使用util.inspect() )是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. JSON.stringify()不显示嵌套的 arrays 和对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM