在javascript中创建动态变量名称

时间:2019-04-14 06:00:01

标签: javascript array.prototype.map

我看到这个标题有几个问题,但是似乎没有一个可以正确回答我的问题。我正在尝试通过json对象进行映射,并将每个属性值插入变量中。

我已经尝试了几乎所有我能找到的其他示例,它们要么抛出了语法错误,要么没有完成我试图做的事情

更新: 为了提供更多背景信息,我使用了一个名为glicko2 https://www.npmjs.com/package/glicko2的npm软件包。 makePlayer(),rankings.updateRatings()、. getRating()、. getRd()、. getVol()都是内置在npm包中的原型方法。 这是完整的文档。如果将其放在js文件中并安装npm软件包,它将为您运行所有计算。本质上,我想做的是编写一个函数,在其中可以将任何播放器数据输入到该函数中,而不必像示例中那样对其进行硬编码并仍然获得计算结果:

var settings = {
  // tau : "Reasonable choices are between 0.3 and 1.2, though the system should
  //       be tested to decide which value results in greatest predictive accuracy."
  tau : 0.5,
  // rating : default rating
  rating : 1500,
  //rd : Default rating deviation
  //     small number = good confidence on the rating accuracy
  rd : 200,
  //vol : Default volatility (expected fluctation on the player rating)
  vol : 0.06
};
var ranking = new glicko2.Glicko2(settings);

// Create players
var Ryan = ranking.makePlayer();
var Bob = ranking.makePlayer(1400, 30, 0.06);
var John = ranking.makePlayer(1550, 100, 0.06);
var Mary = ranking.makePlayer(1700, 300, 0.06);
//We can then enter results, calculate the new ratings...

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);
//... and get these new ratings.

console.log("Ryan new rating: " + Ryan.getRating());
console.log("Ryan new rating deviation: " + Ryan.getRd());
console.log("Ryan new volatility: " + Ryan.getVol());

const allPlayerStats = [
    [{
            "name": "player0",
            "careerSkill": 2080,
            "careerHighestSkill": 1500,
            "careerWins": 260,
            "careerLosses": 100,
            "careerPercent": 72,
            "careerBestLeader": "ZuluShaka",
            "careerAllLeader": [
                "ZuluShaka"
            ],
            "id": 3,
            "highestCareerSkill": 1770,
            "careerSeasonWins": 210,
            "careerSeasonLosses": 21,
            "careerSeasonPercent": 91,
            "teamSkill": 1780,
            "highestTeamSkill": 1500,
            "teamWins": 210,
            "teamLosses": 10,
            "teamPercent": 95,
            "teamBestLeader": "IndiaGandhi",
            "teamAllLeader": [
                "GermanyFrederickBarbarossa"
            ],
            "teamSeasonWins": 20,
            "teamSeasonLosses": 2,
            "teamSeasonPercent": 91,
            "duelSkill": 1780,
            "highestDuelSkill": 1500,
            "duelWins": 10,
            "duelLosses": 45,
            "duelPercent": 72,
            "duelBestLeader": "HungaryMatthias",
            "duelAllLeader": [
                "ZuluShaka"
            ],
            "duelSeasonWins": 210,
            "duelSeasonLosses": 21,
            "duelSeasonPercent": 91,
            "ffaSkill": 1980,
            "highestffaSkill": 1500,
            "ffaWins": 230,
            "ffaLosses": 109,
            "ffaPercent": 67,
            "ffaBestLeader": "MapucheLautaro",
            "ffaAllLeader": [
                "ZuluShaka"
            ],
            "ffaSeasonWins": 25,
            "ffaSeasonLosses": 54,
            "ffaSeasonPercent": 72,
            "careerHighSkill": 2180,
            "teamHighSkill": 1780,
            "duelHighSkill": 1880,
            "ffaHighSkill": 1980
        },
        false
    ],
    [{
            "name": "player1",
            "careerSkill": 2080,
            "careerHighestSkill": 1500,
            "careerWins": 78,
            "careerLosses": 80,
            "careerPercent": 49,
            "careerBestLeader": "SwedenKristina",
            "careerAllLeader": [
                "ZuluShaka",
                "GermanyFrederickBarbarossa"
            ],
            "id": 4,
            "highestCareerSkill": 1330,
            "careerSeasonWins": 40,
            "careerSeasonLosses": 28,
            "careerSeasonPercent": 59,
            "teamSkill": 1990,
            "highestTeamSkill": 1500,
            "teamWins": 65,
            "teamLosses": 87,
            "teamPercent": 42,
            "teamBestLeader": "KoreaSeondeok",
            "teamAllLeader": [
                "GreeceGorgo"
            ],
            "teamSeasonWins": 54,
            "teamSeasonLosses": 43,
            "teamSeasonPercent": 56,
            "duelSkill": 1300,
            "highestDuelSkill": 1500,
            "duelWins": 21,
            "duelLosses": 11,
            "duelPercent": 63,
            "duelBestLeader": "AztecMontezuma",
            "duelAllLeader": [
                "GreeceGorgo"
            ],
            "duelSeasonWins": 34,
            "duelSeasonLosses": 76,
            "duelSeasonPercent": 31,
            "ffaSkill": 1770,
            "highestffaSkill": 1500,
            "ffaWins": 90,
            "ffaLosses": 23,
            "ffaPercent": 80,
            "ffaBestLeader": "BrazilPedroII",
            "ffaAllLeader": [
                "GreeceGorgo"
            ],
            "ffaSeasonWins": 22,
            "ffaSeasonLosses": 15,
            "ffaSeasonPercent": 60,
            "careerHighSkill": 2080,
            "teamHighSkill": 1990,
            "duelHighSkill": 1300,
            "ffaHighSkill": 1780
        },
        false
    ],
    [{
            "name": "player2",
            "careerSkill": 1680,
            "careerHighestSkill": 1500,
            "careerWins": 84,
            "careerLosses": 32,
            "careerPercent": 72,
            "careerBestLeader": "GreeceGorgo",
            "careerAllLeader": [
                "GreeceGorgo"
            ],
            "id": 9,
            "highestCareerSkill": 1760,
            "careerSeasonWins": 260,
            "careerSeasonLosses": 62,
            "careerSeasonPercent": 80,
            "teamSkill": 1890,
            "highestTeamSkill": 1500,
            "teamWins": 65,
            "teamLosses": 44,
            "teamPercent": 60,
            "teamBestLeader": "ChinaQinShiHuang",
            "teamAllLeader": [
                "GreeceGorgo"
            ],
            "teamSeasonWins": 203,
            "teamSeasonLosses": 28,
            "teamSeasonPercent": 88,
            "duelSkill": 1580,
            "highestDuelSkill": 1500,
            "duelWins": 10,
            "duelLosses": 21,
            "duelPercent": 32,
            "duelBestLeader": "KongoMvembaANzinga",
            "duelAllLeader": [
                "GreeceGorgo"
            ],
            "duelSeasonWins": 10,
            "duelSeasonLosses": 32,
            "duelSeasonPercent": 24,
            "ffaSkill": 1550,
            "highestffaSkill": 1500,
            "ffaWins": 70,
            "ffaLosses": 59,
            "ffaPercent": 54,
            "ffaBestLeader": "MacedonAlexander",
            "ffaAllLeader": [
                "GreeceGorgo"
            ],
            "ffaSeasonWins": 43,
            "ffaSeasonLosses": 26,
            "ffaSeasonPercent": 62,
            "careerHighSkill": 1787,
            "teamHighSkill": 1890,
            "duelHighSkill": 1580,
            "ffaHighSkill": 1780
        },
        false
    ]
]



// 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)
));
// the expected output of the above map would look like this and all of // that data comes from the allPlayerStats object:
// var player0 = ranking.makePlayer(1989, 200, 0.06);
// var player1 = ranking.makePlayer(1500, 200, 0.06);
// var player2 = ranking.makePlayer(1750, 300, 0.06);
// then each one of those variables would be listed as shown below in the matches array
 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())
));

0 个答案:

没有答案