这是我的代码:
function fetchAccounts() {
var request = new XMLHttpRequest();
request.open('GET', "account_url", true);
request.onload = function () {
console.log(JSON.parse(request.responseText))
accounts_array = JSON.parse(request.responseText);
console.log("accounts"+accounts_array)
};
request.send();
}
首先console.log
正确地从我的数据库中打印出数据。第二个console.log
用来检查数组。但是,输出是accounts[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
设置accounts_array
结果之间,这出了什么问题?我已accounts_array
声明其他地方。
"accounts"+accounts_array
转换accounts_array
为字符串,然后将其附加到"accounts"
。将数组转换为字符串会toString
在数组上调用join
,该数组会调用,它会遍历数组的所有条目,将它们转换为字符串,然后将它们,
之间插入。显然,您的数组包含具有默认toString
方法的纯对象,因此它们会转换为"[object Object]"
。这就是为什么[object Object]
在它们之间有一系列逗号。
这是一个例子:
const accounts_array = [{}, {}, {}, {}];
console.log("accounts" + accounts_array);
如果要查看数组的内容,请将其记录为自己的内容,例如:
console.log("accounts", accounts_array);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句