有两种潜在的简单的解决方案来转储数组作为字符串。根据您所使用的环境:
...随着现代浏览器都使用JSON:
JSON.stringify(filters);
// returns this
"{"dvals":[{"brand":"1","count":"1"},{"brand":"2","count":"2"},{"brand":"3","count":"3"}]}"
...的东西,如Node.js的,你可以使用console.info()
console.info(filters);
// will output:
{ dvals:
[ { brand: '1', count: '1' },
{ brand: '2', count: '2' },
{ brand: '3', count: '3' } ] }
编辑:
JSON.stringify带有两个可选参数。第三个“空间”参数使漂亮的印刷:
JSON.stringify(
obj, // the object to stringify
replacer, // a function or array transforming the result
spaces // prettyprint indentation spaces
)
例如:
JSON.stringify(filters, null, " ");
// returns this
"{
"dvals": [
{
"brand": "1",
"count": "1"
},
{
"brand": "2",
"count": "2"
},
{
"brand": "3",
"count": "3"
}
]
}"
你使用什么浏览器? 'console'对象仅在某些浏览器或附加组件上可用 – Phil
您使用的浏览器是什么? – chustar
适用于我:http://jsfiddle.net/PxZjr/ –