javascript - create an object containing objects based on JSON data 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):



The initial JSON data response:


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"]++;
                }
            }
        })
    };
}
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);
})