Here is an example that you can type in your console. Super new to Javascript. The example is reproducible by opening a new tab and typing it out in a console (The JSX Fiddle's console feature is in beta, so I'm not sure if it can be trusted)
let clothing = ['card0', 'card1', 'card2', 'card3'];
let timers = {}
let timerObj = {"startTime": null, "pauseTime": null, "elapsedTime": null, "hasSubmitted": false} //Nested object I want for all indices, will manipulate 0th index alone inside for loop
for (var i = 0; i < clothing.length; i++) {
timers[i] = timerObj
if (i == 0) {
timers[i]["startTime"] = Date.now();
}
}
console.log(timers)
What I'm intending to do is, for the 0th index alone, set the timers[0]["startTime"]
as Date.now()
, and for the rest, let the startTime
be null
as defined in the timerObj
.
Strangely, after running the for loop, I see that for all i
, the Date.now()
has been set. I understand that Javascript objects are mutable, but why is why are all indices being set to Date.now()
?
I looked at other Javascript related Object
questions related to a concept call "freezing", not sure I have my basics right.
EDIT: I think this is related the object reference being altered..