Can't call a value from inside an object dynamically based on a variable. ( Reference error: Not defined )
I have multiple objects set up within other objects and would like to call specific values inside of them based on the user input ( a preset value in this example for simplicity ) but I get an error whenever I try to call those values/numbers I want.
let creekchub = {
islandWarrior: {
iw2: {
sb: 850,
tgttos: 1670,
hitw: 2300,
bb: 1985
},
iw1: {
sb: 600,
tgttos: 1850,
hitw: 2100,
bb: 1650
}
}
}
let player = "creekchub";
let server = "islandWarrior";
let event = "iw1";
console.log(player.server.event.bb);
Is my first attempt. My expected output would have been 1650 however I get a error: "Reference Error: islandWarrior not defined" I tried something similar but with a function
typefunction printStats(player,server,event){
console.log(player.server.event.bb);
}
printStats(creekchub,islandWarrior,iw1);
here
but it has a similar issue, simply outputting the same error.
Is there a better/more efficient way to do this, and not get an error? Ty.
Via Active questions tagged javascript - Stack Overflow https://ift.tt/sG6qwi0
Comments
Post a Comment