1

I have 2 objects, player and rooms

I have a ul tag of which i would like to populate the li depending on how many options there are and depending on where the player is.

I have the following

let player = {
  name: "",
  location: "start"
}

let rooms = {
  start: {
       name: "outside",
       test: "testing",
       options:{
            option1: "Option 1",
            option2: "OPtion 2"
       }
      },
    end:{
      name: "outside",
       test: "testing",
       options:{
            option1: "start",
            option2: "end"

        }
    }
}


for (let key in rooms.start.options){
  let option = document.createElement("li");                 
option.innerHTML = (rooms.(player.location).options[key]);
optionsList.appendChild(option)
  console.log();
}

I am unsure how to reference the player location in the last few lines that so I can have either:

option.innerHTML = (rooms.start.options[key]); or

option.innerHTML = (rooms.end.options[key]);

some people are advising to use arrays but I like to reference such as player.name, player.location, etc

Sofyan Thayf
  • 1,322
  • 2
  • 14
  • 26
nibz
  • 11
  • 2

1 Answers1

0

Try This way

using Bracket notation

for (let key in rooms.start.options){
   console.log(rooms[player.location].options[key]); 
}

Hope it helps

sayalok
  • 882
  • 3
  • 15
  • 30