Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logging JavaScript arrays in VS Code terminal

Tags:

vs-code

When I console.log an array who contains objects in VS code terminal,

console.log(`new array is ${state.chats}`);

I get a result like that :

new array is [object Object],[object Object],[object Object],[object Object]

Instead of normal tree where you can see the objects inside like in Chrome Dev Tools :

new array is: [
{ sender: "joseph", message: "my text" },
{ sender: "daniel", message: "my text" },
{ sender: "joseph", message: "my text" }
]

Any way to "fix"/modify this behavior?

like image 238
apollo24 Avatar asked Sep 08 '26 18:09

apollo24


1 Answers

VS Code's terminal uses a command line program like cmd.exe, powershell etc, these tools just put out the string values of your provided variable.

For { sender: "joseph", message: "my text" } that would be [object Object]

One way to get the data as asked would be to convert it to a string with JSON.stringify.

console.log(`new array is ${JSON.stringify(state.chats})`);

like image 156
Jelle Bruisten Avatar answered Sep 12 '26 23:09

Jelle Bruisten