gpt4 book ai didi

javascript - console.log 显示数组对象的内容

转载 作者:IT王子 更新时间:2023-10-29 03:08:35 24 4
gpt4 key购买 nike

我已经尝试使用 console.log,所以我可以看到包含多个对象的数组的内容。但是我收到一条错误消息,提示 console.log is not an object 等。我使用的是 jquery 1.6.2,我的数组是这样的:

filters = {dvals:[{'brand':'1', 'count':'1'},
{'brand':'2', 'count':'2'},
{'brand':'3', 'count':'3'}]}

console.log(filters);

我想做的是将 array(filters) 的内容写到过滤器中的警告框(这就是我认为 console.log 所做的)格式。我该怎么做?

最佳答案

将数组转储为字符串有两种潜在的简单解决方案。根据您使用的环境:

……现代浏览器使用 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 带有两个可选参数。第三个“空格”参数启用 pretty-print :

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"
}
]
}"

关于javascript - console.log 显示数组对象的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7912576/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com