如何在javascript中打印对象数组?

时间:2022-01-09 20:47:35

Lets say I have an array of Object like :

假设我有一个Object数组,如:

var x=[{a:1},{b:2},{c:3}]; 

How do I print it just like as it is. So, it would be printed like

如何打印它就像它一样。所以,它将被打印出来

[{a:1},{b:2},{c:3}]  

3 个解决方案

#1


2  

You can always define your own toString method, but JSON.stringify will give you a close enough result:

您始终可以定义自己的toString方法,但JSON.stringify将为您提供足够接近的结果:

var x=[{a:1},{b:2},{c:3}];

alert(JSON.stringify(x));

#2


0  

You should be able to use JSON.stringify(x)

你应该能够使用JSON.stringify(x)

See here: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify

请参见:https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify

#3


0  

But if you just do this in order to debug, then you'd better use the console with console.log(X).

但是如果你只是为了调试而这样做,那么你最好使用console.log(X)控制台。

#1


2  

You can always define your own toString method, but JSON.stringify will give you a close enough result:

您始终可以定义自己的toString方法,但JSON.stringify将为您提供足够接近的结果:

var x=[{a:1},{b:2},{c:3}];

alert(JSON.stringify(x));

#2


0  

You should be able to use JSON.stringify(x)

你应该能够使用JSON.stringify(x)

See here: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify

请参见:https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify

#3


0  

But if you just do this in order to debug, then you'd better use the console with console.log(X).

但是如果你只是为了调试而这样做,那么你最好使用console.log(X)控制台。