在我调度它们之后,数组变得不确定。记录它们

时间:2015-08-08 05:40:35

标签: jquery arrays json

我正在尝试创建一系列数组,其中包含10个可用于计算的值。

我的代码就像这样(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);

当我进入浏览器时,它正确地控制了.log:

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

谢谢!

2 个答案:

答案 0 :(得分:1)

您的数组仅在成功处理程序中已知。您需要将它们移到该范围之外,以便在该范围之外查看它们。 如果您在成功之后尝试使用console.log vars,它们就会消失。

以下是如何让一个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])

答案 1 :(得分:0)

您需要了解两件事:变量范围和异步回调。你可以在网上找到很多关于这些的内容,所以我只是给出简短的定义:

  • 变量范围是,其中变量可见;为了使程序更容易理解,语言允许你保持部件彼此隔离,而在JS中,每个变量只在你编写(E0002)的函数内部可见,加上在该块内定义的函数
  • 异步回调在定义变量时更改;你不知道AJAX请求需要多长时间,所以你让JS在等待时运行其他代码,当它得到响应时,你的for (UIView *theView in self.subviews) { if ([theView isKindOfClass:[UIView class]]) { for (UIButton *theButton in theView.subviews) { if ([theButton isKindOfClass:[UIButton class]]) { [theButton setSelected:NO]; } } } } 回调就会被激活。

因此,您需要做的第一件事就是通过正确放置var语句,在您的所有代码都能看到它们的范围内定义变量。然后,您需要确保从它们读取的代码在写入它们的代码之前没有运行,通过考虑回调何时运行。