Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display current object value console log

I would like to display the current values of an object's attributes in Javascript.

I did a console.log(object) and it gave me this :

Console log

And it gave me an i blue box that when hovered, it gives me this text object value at left was snapshotted when logged, value below was evaluated just now.

I saw a few previous posts suggesting to convert the log to JSON using console.log(JSON.parse(JSON.stringify(object))); but it just gives me the values in red.

I am more interested in the ones in green.

This actually brings a question : Which of these values are the latest, eventually final ? The ones in red or the ones in green ?

Thanks!

like image 229
user1885868 Avatar asked Mar 13 '23 00:03

user1885868


2 Answers

The green values are the values as they were when you expanded the log.
The red values are the values from the time you logged them.

A simple test in the console

let obj2 = { a: 1, b: 2, c:3, d:4, e:5, f:6 };
setInterval(()=>{
    obj2.a+=0.1;
    obj2.b+=0.01;
});
console.log(obj2);

Provides this capturing image when I expand the logged object
Chrome dev tools clipping

No matter how many times I expand again the values remain and if I want the updated values I need to do another console.log(ob2).

like image 118
Tobias Avatar answered Mar 21 '23 02:03

Tobias


The values in the red box are values when the console.log was called. Green ones are the values after the Object in your console was clicked. If you have a loop, then calling console.log each time in end of the loop will give you the latest values always. If you want only the final value, then after your loop or calculations have ended and the values wont be modified again, call console.log on the object and it will give you the final values.

like image 37
Hendry Avatar answered Mar 21 '23 01:03

Hendry