2015-08-08 58 views
0

我想創建一系列的數組中有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正確:

enter image description here

但是當我嘗試鍵入類似:

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語句?

謝謝!

+0

爲它創建一個小提琴 – dom

回答

1

您的數組只在成功處理程序中知道。您需要將它們移出該範圍以外的範圍才能看到它們。 如果你在成功後嘗試使用console.log這些變量,它們就會消失。

這裏是如何讓一個var保存所有數組,並有一個循環將它們全部更新。

// declare all arrays holding valuable statistics 
var statsArr = { 
    csArray  :[], 
    assistArray :[], 
    killArray :[] 
}; // no comma on the last 
$.ajax({ 
    dataType: "json", 
    type: 'GET', 
    url: 'page.php', 
    success: function(data){ 

    for (var i=0; i<10; i++){ 
     var stats = data.matches[i].participants[0].stats; 
     statsArr.csArray.push(stats.minionsKilled); 
     statsArr.assistArray.push(stats.assists); 
    } 

數組現在在全局範圍內保存在statsArr對象中。

要在以後使用:

var pvpFactor = ((aFactor * statsArr.killArray[i]) 
+0

數組仍然在成功處理程序內,他們不是? – Vranvs

+0

我想你看不到整個代碼,因爲我把它分成兩部分,但我的程序顯示,對應於成功代碼塊的大括號包含在內,基本上包含了我的所有代碼 – Vranvs

+0

它們現在處於全局範圍內。查看更新 – mplungjan

0

你需要明白兩兩件事:變量的作用域和異步回調。你也許可以在網上找到許多關於這些,所以我就放棄簡要的定義:

  • 變量的作用域是其中變量是可見的;使程序更容易理解,語言讓你保持相互隔離的部件,在JS,每一個變量時,才能看到你寫var裏面的功能,加上塊內定義的函數
  • 異步回調改變定義一個變量;你不知道AJAX請求需要多長時間,所以你讓JS在等待的時候運行其他代碼,當它得到響應時,你的回調會觸發。

因此,您需要做的第一件事是通過正確地放置您的var語句來在所有代碼可以看到它們的範圍內定義變量。然後,您需要確保從它們讀取的代碼在寫入它們的代碼之前不會運行,通過考慮回調何時運行。

+0

非常感謝,這清理了很多東西! – Vranvs

相關問題