在控制台中获取详细信息而不是“[object Object]”

Get details in console instead of "[object Object]"

我可能遇到了一个非常简单的问题,但我需要知道如何执行此操作。 我的抱负:

var test_obj = {     
  'test' : 't',
  'test2' : 't2'
}
/*
when i log a object it often gives me somthing like this:
 [object Object]
 
but if i want to take a look into the object, i want to see (for test_obj):
{ 'test' : 't', 'test2' : 't2' }
  
*/

那么我怎样才能记录一个显示内容的对象呢?

将您的对象转换为字符串表示形式。在对象上使用JSON Stringify,然后就可以显示了。试试这个代码

JSON.stringify(test_obj)

您需要读取对象索引: 你可以试试这个:

var keys = Object.keys(test_obj);
    for (var i=0; i<keys.length; i++)
        {
            JSON.stringify(keys[i]);
}

您可以使用 JSON.stringify 和适当的参数来格式化输出。

var test_obj = {     
   'test' : 't',
   'test2' : 't2'
};

document.write('<pre>' + JSON.stringify(test_obj, 0, 4) + '</pre>');