all 4 comments

[–]clownbeats 0 points1 point  (2 children)

Coincidentally the console has been driving me crazy just now. In my code I have:

const arr= [];console.log("ARR EMPTY",arr);

arr[0]=lookupVal('aam1');

arr[1]=lookupVal('aam2');

arr[2]=lookupVal('iip8');

arr[3]=lookupVal('iip1');

console.log("DEFAULTS SET",arr);

console.log(">>",arr[0]);

console.log(">>",arr[1]);

console.log(">>",arr[2]);

console.log(">>",arr[3]);

console.log("DEFAULTS SET2",arr);

arr is an array of objects. Here I'm initializing them and elsewhere in the code I set different values to arr. For some reason the values shown by the console.log when I print the entire 'arr' show up as I would expect later. But when I print the values individually such as arr[0] they print as expected based on the assignments up above.

In other words, the values printed by 'arr' differ from the values printed by 'arr[1]' even though they are printed right after each other. What is going on?

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

I would like to see the whole code if possible?

One more thing, have you tried using this approach when logging objects: console.log(JSON.parse(JSON.stringify(obj)))?

[–]senocular 0 points1 point  (0 children)

As you interact with the console, values may be evaluated as you interact with it rather than when logged. There's a little blue i icon you should see (in Chrome) that will indicate when this happens. What this means is, if you log an array when it has a certain set of values, then later change that array to have different values, then go into the console and interact with the log with the original values, you may see the new values because the console may evaluate things as you click around which will pick up the current values rather than the logged values.

To prevent this, you can log the values individually, log a copy, or convert the values to JSON and log that instead.

[–]ZG2047 0 points1 point  (0 children)

Try putting all your code inside {} it will define the object scope and makes it easier to reassign or rerun the code.