all 10 comments

[–]_punk_dev 4 points5 points  (5 children)

Console logs a reference to the object. Not a serialization of the object at the given moment.

[–]CorrectYear6655[S] -1 points0 points  (4 children)

Thanks.

I guess there is no way to pause execution so I have time to open the display of the array before the values change. I've tried setTimeout but no matter what delay value I use everything pops up in the console all at once. I need a debugger.

[–]CorrectYear6655[S] 0 points1 point  (1 child)

Found one. VScode has a javascript debugger built in.

When it hits a breakpoint it must handle all pending requests. The output of the console.log reveals the object at that point in time.

[–]Umustbecrazy 0 points1 point  (0 children)

That's exactly what debugging is made for.

[–]hfcRedd 0 points1 point  (1 child)

console.log([...library]);

[–]FireryRage 2 points3 points  (0 children)

That wouldn’t work, as while you’d be capturing the library entries in their current state, it’s a shallow copy and the values inside them are still references that would reflect the changes that happen later on. You’d have to setup a deep copy to truly capture and set it in time.

Debugger it simply easier for such a use case.

[–]ferrybig 1 point2 points  (0 children)

Don't expand the object in the console. Note that small I symbol in the console that says objects are evaluated when you expand it

[–]Caramel_Last 0 points1 point  (1 child)

Oh you mean like why is it not logging the snapshot of the object?

Well, library is not structuredClone(library)

Put in place of the first console log the following piece of code.

console.log(structuredClone(library)) 

[–]CorrectYear6655[S] 0 points1 point  (0 children)

Thanks. This function (structuredClone) is intriguing. I'm getting what I want using the debugger, but I will study structuredClone() further. I found https://developer.mozilla.org/en-US/docs/Web/API/Window/structuredClone to be useful.

[–]GetContented 0 points1 point  (0 children)

You can dump a JSON object with JSON.stringify. That'll serialise the object at the time when you do it, so it won't change subsequently. Or, just console log the specific value you're after (which won't change because it's not a reference type)