Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

227
Views
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.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!