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"]++;
 }
 }
 })
 };
}
janos
113k15 gold badges154 silver badges396 bronze badges
asked Aug 1, 2016 at 20:04
\$\endgroup\$
2
  • 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\$ Commented Aug 1, 2016 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\$ Commented Aug 1, 2016 at 21:24

1 Answer 1

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);
})
answered Aug 1, 2016 at 21:13
\$\endgroup\$
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.