0

我正在尝试构建一个包含 NFL 2012 赛程的对象。我在网上找到了它的 CSV 并且可以解析它,除非我试图每周添加游戏。当我在每周执行代码后控制台记录时间表时,只会得到一场比赛(最后一场),而不是他们应该拥有的 16 场(左右,取决于轮空)。

注意:通常我将我的原型放在源文件中而不是在这里,但为了清楚起见,我将它们包括在内。

奖励:我会在完成后发布该对象。: D

这是我的代码:

String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g,"");
}

String.prototype.capitalize = function() { 
    return this.substr(0, 1).toUpperCase() + this.substr(1);
}

Object.prototype.getKeys = function(obj){
    var keys = [];
    for(var key in obj){
        keys.push(key);
    }
    return keys;
}

Array.prototype.contains = function(obj) {
    var i = this.length;
    while (i--) {
        if (this[i] == obj) {
            return true;
        }
   }
   return false;
}

var AppController = {};

//create a schedule object
AppController.Schedule = {};
var schedule = AppController.Schedule;

    var scheduleArray = nflschedule.split(":::");

    //set a game count so we can give each game an id
    var gameCount = -1;

    for(var i=0; i<scheduleArray.length; i++) { 

        gameCount++;

        //filter out first line
        if (i>0) { 

            //get a game
            var thisGameItems = scheduleArray[i].split(",");

            //game date
            var gameDate = thisGameItems[0];

            //week number
            var colonSplitPattern = new RegExp("^[^:]+(?=:)");
            var weekNumPattern=new RegExp("\Week(.*)");
            var weekNum = "Week" + thisGameItems[2].match(colonSplitPattern).toString().match(weekNumPattern)[1].trim();
            //get teams and game day
            var homeTeam = thisGameItems[thisGameItems.length-3].trim();
            var visitingTeam = thisGameItems[thisGameItems.length-2].trim();
            var weekDay =   thisGameItems[thisGameItems.length-1].trim().capitalize();

            //check Schedule and see if we have this weekNum as key, if not add it
            var gameWeeks = schedule.getKeys();
            if (gameWeeks.contains(weekNum) == false) { 
                schedule[weekNum] = {};
            }

            var thisWeeksGames = schedule[weekNum];
            //add games to the game week
            thisWeeksGames[gameCount] = {

                GameDate: gameDate,
                GameWeek: weekNum,
                HomeTeam: homeTeam,
                VisitingTeam: visitingTeam,
                GameDay: weekDay,
                HomeTeamScore : "",
                VistingTeamScore : ""

            }

        }
    }

console.log(schedule);
4

0 回答 0