So here’s the problematic code
var board = [
{
grdNbr: 1,
value: 0,
},
{
grdNbr: 2,
value: 0,
},
{
grdNbr: 3,
value: 0,
},
{
grdNbr: 4,
value: 0,
},
{
grdNbr: 5,
value: 0,
},
{
grdNbr: 6,
value: 0,
},
{
grdNbr: 7,
value: 0,
},
{
grdNbr: 8,
value: 0,
},
{
grdNbr: 9,
value: 0,
},
]
I tried declaring objects as arrays, but I don’t really get how it works, so please explain it to me. So, back to the topic. When I check the value it shows undefined
(the code is below)
switch(board.grdNbr){
case 1:
board.value = 1
console.log(board.value)
bg.bg1 = x
msg.channel.send(boardDRW)
turn++
break;
...
}
After that, in the console log the value for value
is 1, but before that the value is undefined
. Any idea how can I fix this?
Your problem is your board variable is an array of object, and you try to access directly a parameter of the array without specifying what object of the array you wanna access.
Instead of trying to access a value with
board.value
, you should useboard[index].value
with index being the index of the object in that list (exemple :board[0].value
)Before I begin, I want to thank you that you spent some time helping me out. I found a solution to my problem with a different code, so I’m very sorry for wasting your time.
Have a nice day!