NFL日程的javascript对象

时间:2012-08-25 11:30:37

标签: javascript object

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

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

BONUS:我会在完成后发布对象。 :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);

0 个答案:

没有答案