Responsive Ad Area

Share This Post

test

creating dynamic variable names in javascript

I saw there were several questions with this title but not one of them seemed to properly answer my question. I am trying to map through a json object and plug each property value into a variable.

Ive tried just about all of the other examples I could find and they either threw syntax errors or did not accomplish what Im trying to do

This is a hard coded example of what I am trying to accomplish. Instead of writing out each variable separately I want to use .map() to dynamically plug in any players stats into this format

var John = ranking.makePlayer(1550, 100, 0.06);
var Mary = ranking.makePlayer(1700, 300, 0.06);

var matches = [];
matches.push([Ryan, Bob, 1]); //Ryan won over Bob
matches.push([Ryan, John, 0]); //Ryan lost against John
matches.push([Ryan, Mary, 0.5]); //A draw between Ryan and Mary
ranking.updateRatings(matches);


// heres what I have so far. What I want to happen is have playerName[0].name 
// be the variable name and set that equal to the rest of the equation. That
// way I can plug in the variable player0 into the matches array to run my 
// calculations. The problem I am having is when the code is getting to the 
// const matches array it is saying that player0 is undefined.
// heres what I do know: 
// allPlayerStats is a properly formatted json object.
// I have console logged every bracket/dot notation property separately to know 
// that all of them are written in proper syntax. 
 allPlayerStats.map(playerName => (
[playerName[0].name] = ranking.makePlayer(playerName[0].ffaSkill, playerName[0].ffaRatingDiviation, playerName[0].ffaVolatility)
));
 const matches = [
[player0, player1, 1], 
[player0, player2, 1], 
[player1, player2, 1]];

ranking.updateRatings(matches)

allPlayerStats.map(playerName => (
console.log(`${playerName[0].name} new rating: ` + playerName[0].name.getRating()),
console.log(`${playerName[0].name} new rating deviation: ` + playerName[0].name.getRd()),
console.log(`${playerName[0].name} new volatility: ` + playerName[0].name.getVol())
));


creating dynamic variable names in javascript
creating dynamic variable names in javascript
test
{$excerpt:n}

Share This Post

Leave a Reply

Your email address will not be Publishedd. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Skip to toolbar