'Index of array exists when logged to console but does not exist when accessed directly

I have a 2-dimensional array that stores "chunks" as canvases in a game. I've encountered a weird problem that seems to have manifested itself seemingly without me changing anything. The range can go negative, so that could be a cause.

When I run a loop to go through and log every index of the array, it only actually logs index -1. When logging the entire 2-dimensional array, indexes -1, 0, and 1 exist. However when I access index 0 of said array it returns undefined:

Code used to log array

Code used to log array

Actual logged results

Logged results

It's clear that index 0 does exist in the array, but when accessed directly it does not seem to exist and a loop through every index does not think index 0 exists.

This is a pretty bad issue since now the game will re-render a bunch of canvases, killing fps, and making the game unplayable. I don't know why this is happening, and I really have no idea why this is happening. This wasn't happening before and I haven't touched the code, but I discovered my fps was dying and eventually found that the cause was the continuous re-rendering of chunks that already existed.

I've tried a few things, including:

  • Changing all array indexes to integers with parseInt()
  • Trying to access the index as a string
  • Removing all other code that manipulates the MAPS variable

It doesn't seem to be easily reproducible since I don't know the cause, but I will try and find the exact reason why it seems to do this.

Update: It seems to work when directly accessed through console, however in the function it doesn't work.

UPdate

Ignore the two logs between the entry and the output, that's caused by a bit of lag.

Update 2: The length of the array appears to be negative, though it still has positive indexes.

update2

Update 3: I just checked GitHub Desktop and I have not changed anything related to the array, and it does not happen on the older code. I don't know what could have made this suddenly happen.



Solution 1:[1]

It turns out the "handy trick" to clearing an array does not work - setting the length of an array to 0 does not always clear it. It ends up doing something and does not seem to work correctly in my case.

Also it turns out I was accidentally clearing portions of the array.

Sorry.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Yes64