4
\$\begingroup\$

Based on JSON data I receive, I am trying to track a team's home and away games. The JSON data is stored in $scope.gameSchedules and has "team1" for home games and "team2" for away games. I need to initially create an object with each team's name and the only way I can get all the teams' names is via the "team1/team2" values in the initial JSON data (thus the two loops below). This was my working solution to create an object that looks like this (called homeVsAwayGames):

enter image description here

The initial JSON data response: enter image description here

Here is my code. Is there a better way to do it, or more concise, perhaps using underscore (not necessary)? I feel that perhaps I have too many lines of code and it can be refactored:

 $scope.getSchedules = function() {
        return TeamsScheduleService.getTeamSchedules().then(function(response) {
            $scope.gamesSchedule = response.data.games;
        }, function(err) {
            console.log("there was an error getting the schedules");
        })
    }
    $scope.homeVsAwaySchedules = {};
    $scope.getSchedules().then(trackHomeVsAway).then(function(){
        console.log($scope.homeVsAwaySchedules);
    })
    //TODO: ng-repeat over the homeVsAwaySchedules Object to display in the view. 

    function trackHomeVsAway() {
        //initialize homeVsAwaySchedules object in separate loop for efficiency and readability, instead of trying to initialize + tally at the same time.
        $scope.gamesSchedule.forEach(function(gameData) {
          var homeTeam = gameData.team1;
          var awayTeam = gameData.team2;
            if (homeTeam) {
                if (!$scope.homeVsAwaySchedules[homeTeam]) {
                    $scope.homeVsAwaySchedules[homeTeam] = {
                        "Home Games": 0,
                        "Away Games": 0
                    };
                }
            }
            if (awayTeam) {
                if (!$scope.homeVsAwaySchedules[awayTeam]) {
                    $scope.homeVsAwaySchedules[awayTeam] = {
                        "Home Games": 0,
                        "Away Games": 0
                    };
                }
            }
        })

        $scope.gamesSchedule.forEach(function(gameData) {
          var homeTeam = gameData.team1;
          var awayTeam = gameData.team2;
            if (homeTeam) {
                if ($scope.homeVsAwaySchedules[homeTeam]) {
                    $scope.homeVsAwaySchedules[homeTeam]["Home Games"]++;
                }
            }
            if (awayTeam) {
                if ($scope.homeVsAwaySchedules[awayTeam]) {
                    $scope.homeVsAwaySchedules[awayTeam]["Away Games"]++;
                }
            }
        })
    };
}
\$\endgroup\$
  • 1
    \$\begingroup\$ Do you have control over how the initial data structure is injected into $scope? In other words why go through all this is you can start with a better data structure? \$\endgroup\$ – Mike Brant Aug 1 '16 at 21:22
  • \$\begingroup\$ If I understand you correctly, no. It's from a third-party. I'm getting json response from a third party API and I have to change it to make it easy to use NG repeat on \$\endgroup\$ – devdropper87 Aug 1 '16 at 21:24
4
\$\begingroup\$

You could dry up this code by adding some helper functions.

For example instead of this:

$scope.gamesSchedule.forEach(function(gameData) {
  var homeTeam = gameData.team1;
  var awayTeam = gameData.team2;
    if (homeTeam) {
        if (!$scope.homeVsAwaySchedules[homeTeam]) {
            $scope.homeVsAwaySchedules[homeTeam] = {
                "Home Games": 0,
                "Away Games": 0
            };
        }
    }
    if (awayTeam) {
        if (!$scope.homeVsAwaySchedules[awayTeam]) {
            $scope.homeVsAwaySchedules[awayTeam] = {
                "Home Games": 0,
                "Away Games": 0
            };
        }
    }
})

You could write using a helper function:

$scope.gamesSchedule.forEach(function(gameData) {
    function checkTeam(team) {
        if (team) {
            if (!$scope.homeVsAwaySchedules[team]) {
                $scope.homeVsAwaySchedules[team] = {
                    "Home Games": 0,
                    "Away Games": 0
                };
            }
        }
    }
    checkTeam(gameData.team1);
    checkTeam(gameData.team2);
})

Similarly, instead of this:

$scope.gamesSchedule.forEach(function(gameData) {
  var homeTeam = gameData.team1;
  var awayTeam = gameData.team2;
    if (homeTeam) {
        if ($scope.homeVsAwaySchedules[homeTeam]) {
            $scope.homeVsAwaySchedules[homeTeam]["Home Games"]++;
        }
    }
    if (awayTeam) {
        if ($scope.homeVsAwaySchedules[awayTeam]) {
            $scope.homeVsAwaySchedules[awayTeam]["Away Games"]++;
        }
    }
})

You could write using a helper function:

$scope.gamesSchedule.forEach(function(gameData) {
    function updateTeamSchedule(team, games) {
        if (team) {
            if ($scope.homeVsAwaySchedules[team]) {
                $scope.homeVsAwaySchedules[team][games]++;
            }
        }
    }
    updateTeamSchedule(gameData.team1);
    updateTeamSchedule(gameData.team2);
})
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.