3

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?

asked Jan 30, 2020 at 11:59
2
  • Can you show the array? Or a mock of one like it? Commented Jan 30, 2020 at 12:14
  • Hi, added the way it should look in the post Commented Jan 30, 2020 at 12:20

2 Answers 2

3

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})`);

answered Jan 30, 2020 at 12:25
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, got it and working. Minor syntax fix: console.log(new array is (${JSON.stringify(state.chats)}));
0

You must search by print array of objects in js. At these link stay your answer (How to print object array in JavaScript?) you can use JSON.stringify(yourarray).

For more information about go to: https://developer.mozilla.org/ca/docs/Web/JavaScript/Referencia/Objectes_globals/JSON/stringify

var test = [{name:"pepe",age:21},{name:"juan",age:11},
 {name:"aurelio",age:31},{name:"evaristo",age:55},
 {name:"fermin",age:35}];
console.log(JSON.stringify(test));
rioV8
29.7k4 gold badges48 silver badges68 bronze badges
answered Jan 30, 2020 at 12:33

2 Comments

var test = [{name:"pepe",age:21},{name:"juan",age:11},{name:"aurelio",age:31},{name:"evaristo",age:55},{name:"fermin",age:35}];
console.log(JSON.stringify(test));

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.