我想創建一系列的數組中有10個值,我可以用它們來計算。陣列變得undefined後console.log他們
我的代碼是像這樣的(JS):
$.ajax({
dataType: "json",
type: 'GET',
url: 'page.php',
success: function(data){
// declare all arrays holding valuable statistics
var csArray = [];
var assistArray = [];
var killArray = [];
var gameTimeArray = [];
var championArray = [];
var deathArray = [];
var jungleCSArray = [];
var counterJungleCSArray = [];
var roleArray = [];
var laneArray = [];
var crowdControlArray = [];
for (i=0; i<10; i++){
var value = data.matches[i].participants[0].stats.minionsKilled;
csArray[i] = value;
}
for (i=0; i<10; i++){
var value = data.matches[i].participants[0].stats.assists;
assistArray[i] = value;
}
for (i=0; i<10; i++){
var value = data.matches[i].participants[0].stats.kills;
killArray[i] = value;
}
for (i=0; i<10; i++){
var value = data.matches[i].matchDuration;
gameTimeArray[i] = value;
}
for (i=0; i<10; i++){
var value = data.matches[i].participants[0].championId;
championArray[i] = value;
}
for (i=0; i<10; i++){
var value = data.matches[i].participants[0].stats.deaths;
deathArray[i] = value;
}
for (i=0; i<10; i++){
var value = data.matches[i].participants[0].stats.neutralMinionsKilledTeamJungle;
jungleCSArray[i] = value;
}
for (i=0; i<10; i++){
var value = data.matches[i].participants[0].stats.neutralMinionsKilledEnemyJungle;
counterJungleCSArray[i] = value;
}
for (i=0; i<10; i++){
var value = data.matches[i].participants[0].timeline.role;
roleArray[i] = value;
}
for (i=0; i<10; i++){
var value = data.matches[i].participants[0].timeline.lane;
laneArray[i] = value;
}
for (i=0; i<10; i++){
var value = data.matches[i].participants[0].stats.totalTimeCrowdControlDealt;
crowdControlArray[i] = value;
}
console.log(csArray);
console.log(assistArray);
console.log(killArray);
console.log(gameTimeArray);
console.log(championArray);
console.log(deathArray);
console.log(jungleCSArray);
console.log(counterJungleCSArray);
console.log(roleArray);
console.log(laneArray);
console.log(crowdControlArray);
當我去我的瀏覽器,它console.logs正確:
但是當我嘗試鍵入類似:
console.log(killArray[1]);
我收到以下消息:
ReferenceError: killArray is not defined.
最終,我試圖在下游計算中使用這些數字,並且得到「NaN」回報。經過故障排除並確定我的數組似乎不再存在,我想它可能試圖將空值添加到其他空值並返回NaN。
我的計算方法如下:
//ALGORITHM START
for (i=0, i<10; i++;){
switch(roleArray[i]) {
case "DUO":
//assumes top lane duo
var role = "L";
console.log(role);
break;
case "DUO_CARRY":
//assumes bot lane adc
var role = "A";
console.log(role);
break;
case "DUO_SUPPORT":
//assumes bot lane support
var role = "S";
console.log(role);
break;
case "SOLO":
//assumes mid or top solo
var role = "L";
console.log(role);
break;
case "NONE":
//assumes jungle, check lane
var role = "J";
console.log(role);
break;
}
}
switch(role){
case "L":
var aFactor = 1.00; //kills
var bFactor = 0.66; //assists
var cFactor = 0.75; //deaths
var dFactor = 0.80; //cs
var eFactor = 0.20; //jungle cs
var fFactor = 0.55; //counter jungle cs
var gFactor = 0.50; //cc time
break;
case "A":
var aFactor = 1.00;
var bFactor = 0.10;
var cFactor = 1.00;
var dFactor = 1.00;
var eFactor = 0.20;
var fFactor = 0.30;
var gFactor = 0.33;
break;
case "S":
var aFactor = 0.20;
var bFactor = 2.00;
var cFactor = 0.33;
var dFactor = 0.10;
var eFactor = 0.33;
var fFactor = 0.55;
var gFactor = 2.00;
break;
case "J":
var aFactor = 1.00;
var bFactor = 1.50;
var cFactor = 0.75;
var dFactor = 0.33;
var eFactor = 1.20;
var fFactor = 1.60;
var gFactor = 1.00;
break;
}
//FORMULA
for (i=0; i>10; i++){
var pvpFactor = ((aFactor * killArray[i]) + (bFactor * assistArray[i]))/(cFactor * deathArray[i]);
var pveFactor = ((dFactor * csArray[i]) + (eFactor * jungleCSArray[i]) + (fFactor * counterJungleCSArray[i]))/(gameTimeArray[i]);
var auxFactor = (gFactor * crowdControlArray[i]);
var rawHMC = (0.8 * pvpFactor) + (0.4 * pveFactor) + (2.5 * auxFactor)
var HMCarray = [];
console.log (rawHMC);
HMCarray[i] = rawHMC;
}
}
});
});
The console.log (rawHMC);
返回NaN值。
基本上我的問題是:爲什麼我不能console.log我的數組,爲什麼我得到一個NaN返回?這是我的數組循環或我的switch語句?
謝謝!
爲它創建一個小提琴 – dom