Battle_707 Battle_707 - 10 months ago 49
Javascript Question

Why is my object restructured?

I have hit a bit of a snafu. I have a global variable (I wish I could avoid it, but I don't see how) that at some point corrupts. I have looked all over my code and nowhere is the variable manipulated other than where and how I want it too. To make things even odder, when I put the following line in my script (place seems inconsequential)

console.log(_var); debugger;
and then right after type in
in the console myself, the first _var show the correct structure, but the second doesn't...

Here's was the original object looks like (and is supposed to look like):

[1: Array[2], 4: Array[2], 5: Array[2], 7: Array[2], 8: Array[2], 9: Array[2], 10: Array[2], 11: Array[2], 12: Array[2], 13: Array[2], 14: Array[2]]

And here is what the object looks like after the breakpoint:

[undefined × 1, Array[2], undefined × 2, Array[2], Array[2], undefined × 1, Array[2], Array[2], Array[2], Array[2], Array[2], Array[2], Array[2], Array[2]]

I can't seem to figure out why this is happening, but when I loop through this object in a function that's called based on a user action at a later point, this is giving me issues.

Thanks a lot for your help.

Answer Source

There's no problem here. You're just seeing two different ways of displaying the same array. console.log is labeling elements with their indexes and omitting unassigned array indexes, while just typing _var into the console is showing unassigned indexes as undefined × howevermany.

For anyone who thinks this doesn't look like a Javascript array, it's simple to reproduce this behavior in the Chrome console:

Example session