0

I am trying to pass a specific string into "statLocation" to pull data from a javascript object to no avail. This is what I'm attempting:

const statLocation = ".level"

Statistics.getAllStatisticsForGame(gameObj)
        .then(stats =>{
            for(var i = 0; i <stats.length; i++){
                console.log(stats[i] + statLocation );
            }

        }).catch(console.error);

Below is how I normally would hardcode it but I'm trying to make it dynamic so I can grab specific data and reuse the same method:

Statistics.getAllStatisticsForGame(gameObj)
        .then(stats =>{
            for(var i = 0; i <stats.length; i++){
                console.log(stats[i].level);
            }

        }).catch(console.error);

How would I be able to add this string to the call? Any advice much appreciated

Isabel
  • 19
  • 1

1 Answers1

1

You can use bracket notation with objects as well

const statLocation = "level";//notice no dot
Statistics.getAllStatisticsForGame(gameObj)
        .then(stats =>{
            for(var i = 0; i <stats.length; i++){
                console.log(stats[i][statLocation]);
            }

        }).catch(console.error);
vityavv
  • 1,482
  • 12
  • 23