Accessing double key array from JS

Game development with Board Game Arena Studio
Post Reply
tony58uk
Posts: 12
Joined: 23 March 2023, 20:09

Accessing double key array from JS

Post by tony58uk »

Hi all,

I'm having real problems. I want to test if a value exists at a certain location within a double-key array.

My code is as follows (JS)

Code: Select all

if ((board[(die_x + 1)][die_y] === undefined) &&
                        (board[(die_x + 2)][die_y] === undefined) &&
                        (board[(die_x + 3)][die_y] === undefined)) {

                            return_value = true;

                        }
                        
I keep getting type errors (TypeError: Cannot read properties of undefined (reading '6'))

I'm missing something silly but cannot work it out!!!

Thanks
User avatar
robinzig
Posts: 428
Joined: 11 February 2021, 18:23

Re: Accessing double key array from JS

Post by robinzig »

Presumably the outer `board` array is not long enough for the first index (`die_x + 2` and so on) to exist at some point.

Beyond that, which is obvious from the error message, it's on you to debug because we've got no idea what's in that array or what values `die_x` etc might have.
User avatar
RicardoRix
Posts: 2175
Joined: 29 April 2012, 23:43

Re: Accessing double key array from JS

Post by RicardoRix »

use the debugger; statement (put it just before the code to analyse) and the F12 console. You can now single-step through your code.
User avatar
AxeCrazy
Posts: 44
Joined: 15 March 2014, 01:07

Re: Accessing double key array from JS

Post by AxeCrazy »

I had a simulair issue before.
die_x + 1 =>
Are you sure die_x is an integer?
I always put a parseInt() round it these days
if die_x = 4 and you add 1 it might result in 41 (stingwise addition) in stead of 5 which you assume it would be
tony58uk
Posts: 12
Joined: 23 March 2023, 20:09

Re: Accessing double key array from JS

Post by tony58uk »

Okay thanks all, I'll have a look at some of those suggestions tomorrow.
Post Reply

Return to “Developers”