Undefined value with declared values JavaScript

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?

40 thoughts on “Undefined value with declared values JavaScript”

  1. 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 use board[index].value with index being the index of the object in that list (exemple : board[0].value)

    Reply
  2. 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!

    • Abyzls
    Reply

Leave a Comment