0

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?

2
  • 1
    Board is a JSONArray you need need specify the position like board[0].grdNbr Commented Feb 19, 2021 at 19:25
  • I answered the Why you get an undefined value, but if you edit your code and contextualize what you try to achieve I will update my post to help you achieve it Commented Feb 19, 2021 at 19:31

2 Answers 2

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)

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.