0

I need to setup a variable and use it to get an item from an array

var teamAbbr = "PHI";
var getItemArray = nflLiveStatsTeam.teamAbbr;
console.log(getItemArray); // log is unidentified

Using variable above gives me unidentified, but when i console.log below its fine

nflLiveStatsTeam.PHI  // log give me the PHI array

I get the nflLiveStatsTeam.PHI array items

1 Answers1

0

Wrap the variable in brackets, and the value of the variable will be used instead of the name of the variable itself.

let nflLiveStatsTeam = {
 "PHI" : [
  "test",3,4,5
 ]
};
var teamAbbr = "PHI";
var getItemArray = nflLiveStatsTeam[teamAbbr];
console.log(getItemArray);
imvain2
  • 15,480
  • 1
  • 16
  • 21