当我在 VS 代码终端中 console.log 一个包含对象的数组时,
console.log(`new array is ${state.chats}`);
我得到这样的结果:
new array is [object Object],[object Object],[object Object],[object Object]
而不是像在 Chrome 开发工具中那样你可以看到里面的对象的普通树:
new array is: [
{ sender: "joseph", message: "my text" },
{ sender: "daniel", message: "my text" },
{ sender: "joseph", message: "my text" }
]
有什么方法可以“修复”/修改此行为?
最佳答案
VS Code 的终端使用命令行程序,如 cmd.exe、powershell 等,这些工具只是输出您提供的变量的字符串值。
对于 { sender: "joseph", message: "my text"}
这将是 [object Object]
按照要求获取数据的一种方法是使用 JSON.stringify 将其转换为字符串。
console.log(`新数组是 ${JSON.stringify(state.chats})`);
关于visual-studio-code - 在 VS Code 终端中记录 JavaScript 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59985590/