0

I have little issue with my logic here.

I have an accordion which I took a picture

enter image description here

the headers LIVE BETTING, MLB, MLB Props and NBA are here in the HTML (actually is a list of sports of almost 25 items)

  <accordion>
    <accordion-group ng-repeat="sport in sports">
      <accordion-heading>
        <div ng-click="addSportToLines(sport);">
          <span>{{sport.name}}</span>
          <span class="badge">{{sport.leagues.length}}</span>
        </div>
      </accordion-heading>

      <div>
        <a ng-repeat="league in sport.leagues"
           ng-click="addLeagueToLines(league)">
           <span>{{league.name}}</span>
           <span class="badge">{{league.offeringsAvailable}}</span>
        </a>
      </div>
    </accordion-group>
  </accordion>

I need to delete the first <span class=badge> which contains {{sport.leagues.length}} and put the sum of the {{league.offeringsAvailable}} there. The {{league.offeringsAvailable}} is the number in gray badge, that as you can see in MLB goes: 72,30,30,8,30

so in the yellow badge, instead of put sport.leagues.length I need the sum of league.offeringsAvailable

here is the controller for sports

SportsFactory.getSportsWithLeagues().then(function(sports) {
  $scope.sports = sports;
  console.log(angular.toJson($scope.sports, 'pretty'));
});

with this console.log(angular.toJson($scope.sports, 'pretty')); I get this json

as you see, the header, for example: LIVE BETTING which is the first one, contains an array leagues with a prop named offeringsAvailable, so I need to take every single offeringsAvailable inside let's say LIVE BETTING, and put the total amount in a $scope variable in order to display it in the DOM.

[
  {
    "id": 26,
    "name": "LIVE BETTING",
    "priority": 0,
    "leagues": [
      {
        "id": "3042",
        "composedId": "3042G",
        "name": "NBA - Live",
        "lineType": "G",
        "part": 0,
        "offeringsAvailable": 3,
        "sport": {
          "id": 26
        }
      },
      {
        "id": "3042",
        "composedId": "3044Q",
        "name": "NBA - Live (2Q)",
        "lineType": "Q",
        "part": 2,
        "offeringsAvailable": 2,
        "sport": {
          "id": 26
        }
      },
      {
        "id": "3042",
        "composedId": "3045Q",
        "name": "NBA - Live (3Q)",
        "lineType": "Q",
        "part": 3,
        "offeringsAvailable": 2,
        "sport": {
          "id": 26
        }
      },
      {
        "id": "3042",
        "composedId": "3046Q",
        "name": "NBA - Live (4Q)",
        "lineType": "Q",
        "part": 4,
        "offeringsAvailable": 2,
        "sport": {
          "id": 26
        }
      },
      {
        "id": "3068",
        "composedId": "3068G",
        "name": "Soccer - Live",
        "lineType": "G",
        "part": 0,
        "offeringsAvailable": 41,
        "sport": {
          "id": 26
        }
      },
      {
        "id": "3295",
        "composedId": "3295G",
        "name": "MLB - Live",
        "lineType": "G",
        "part": 0,
        "offeringsAvailable": 19,
        "sport": {
          "id": 26
        }
      },
      {
        "id": "3314",
        "composedId": "3314G",
        "name": "Tennis - Live",
        "lineType": "G",
        "part": 0,
        "offeringsAvailable": 1,
        "sport": {
          "id": 26
        }
      },
      {
        "id": "6250",
        "composedId": "6250G",
        "name": "NHL - Live",
        "lineType": "G",
        "part": 0,
        "offeringsAvailable": 4,
        "sport": {
          "id": 26
        }
      },
      {
        "id": "6250",
        "composedId": "6252P",
        "name": "NHL - Live (2P)",
        "lineType": "P",
        "part": 2,
        "offeringsAvailable": 2,
        "sport": {
          "id": 26
        }
      },
      {
        "id": "6250",
        "composedId": "6253P",
        "name": "NHL - Live (3P)",
        "lineType": "P",
        "part": 3,
        "offeringsAvailable": 2,
        "sport": {
          "id": 26
        }
      },
      {
        "id": "6711",
        "composedId": "6711G",
        "name": "NCAA Baseball - Live",
        "lineType": "G",
        "part": 0,
        "offeringsAvailable": 1,
        "sport": {
          "id": 26
        }
      }
    ],
    "img": "images/icons/icon_live_betting.svg"
  },
  {
    "id": 6,
    "name": "MLB",
    "priority": 1,
    "leagues": [
      {
        "id": "1496",
        "composedId": "1496G",
        "name": "MLB",
        "lineType": "G",
        "part": 0,
        "offeringsAvailable": 72,
        "sport": {
          "id": 6
        }
      },
      {
        "id": "1496",
        "composedId": "1497I",
        "name": "MLB (1I)",
        "lineType": "I",
        "part": 1,
        "offeringsAvailable": 30,
        "sport": {
          "id": 6
        }
      },
      {
        "id": "1496",
        "composedId": "1498I",
        "name": "MLB (2I)",
        "lineType": "I",
        "part": 2,
        "offeringsAvailable": 30,
        "sport": {
          "id": 6
        }
      },
      {
        "id": "1721",
          "id": 6
        }
      },
      {
        "id": "2012",
        "composedId": "2012T",
        "name": "MLB - Alternate Runlines",
        "lineType": "T",
        "part": 0,
        "offeringsAvailable": 30,
        "sport": {
          "id": 6
        }
      }
    ],
    "img": "images/icons/icon_mlb.svg"
  },
  {
    "id": 41,
    "name": "MLB  Props",
    "priority": 2,
    "leagues": [
      {
        "id": "6726",
        "composedId": "6726G",
        "name": "MLB Props - Runs+Hits+Error",
        "lineType": "G",
        "part": 0,
        "offeringsAvailable": 15,
        "sport": {
          "id": 41
        }
      },
      {
        "id": "6727",
        "composedId": "6727G",
        "name": "MLB Props - Team to score 1st",
        "lineType": "G",
        "part": 0,
        "offeringsAvailable": 15,
        "sport": {
          "id": 41
        }
      },
      {
        "id": "6728",
        "composedId": "6728G",
        "name": "MLB Props - 1st inning run?",
        "lineType": "G",
        "part": 0,
        "offeringsAvailable": 15,
        "sport": {
          "id": 41
        }
      },
      {
        "id": "6731",
        "composedId": "6731G",
        "name": "MLB Props - 1st Called pitch",
        "lineType": "G",
        "part": 0,
        "offeringsAvailable": 30,
        "sport": {
          "id": 41
        }
      },
      {
        "id": "6732",
        "composedId": "6732G",
        "name": "MLB Props - Players Matchups",
        "lineType": "G",
        "part": 0,
        "offeringsAvailable": 30,
        "sport": {
          "id": 41
        }
      },
      {
        "id": "6741",
        "composedId": "6741G",
        "name": "MLB Props - 1st inning line",
        "lineType": "G",
        "part": 0,
        "offeringsAvailable": 15,
        "sport": {
          "id": 41
        }
      }
    ],
    "img": "images/icons/icon_mlb.svg"
  },
  ... "composedId": "1721T",
        "name": "MLB - Grand Salami",
        "lineType": "T",
        "part": 0,
        "offeringsAvailable": 8,
        "sport": {
          "id": 6
        }
      },
      {
        "id": "2012",
        "composedId": "2012T",
        "name": "MLB - Alternate Runlines",
        "lineType": "T",
        "part": 0,
        "offeringsAvailable": 30,
        "sport": {
          "id": 6
        }
      }
    ],
    "img": "images/icons/icon_mlb.svg"
  },
  {
    "id": 41,
    "name": "MLB  Props",
    "priority": 2,
    "leagues": [
      {
        "id": "6726",
        "composedId": "6726G",
        "name": "MLB Props - Runs+Hits+Error",
        "lineType": "G",
        "part": 0,
        "offeringsAvailable": 15,
        "sport": {
          "id": 41
        }
      },
      {
        "id": "6727",
        "composedId": "6727G",
        "name": "MLB Props - Team to score 1st",
        "lineType": "G",
        "part": 0,
        "offeringsAvailable": 15,
        "sport": {
          "id": 41
        }
      },
      {
        "id": "6728",
        "composedId": "6728G",
        "name": "MLB Props - 1st inning run?",
        "lineType": "G",
        "part": 0,
        "offeringsAvailable": 15,
        "sport": {
          "id": 41
        }
      },
      {
        "id": "6731",
        "composedId": "6731G",
        "name": "MLB Props - 1st Called pitch",
        "lineType": "G",
        "part": 0,
        "offeringsAvailable": 30,
        "sport": {
          "id": 41
        }
      },
      {
        "id": "6732",
        "composedId": "6732G",
        "name": "MLB Props - Players Matchups",
        "lineType": "G",
        "part": 0,
        "offeringsAvailable": 30,
        "sport": {
          "id": 41
        }
      },
      {
        "id": "6741",
        "composedId": "6741G",
        "name": "MLB Props - 1st inning line",
        "lineType": "G",
        "part": 0,
        "offeringsAvailable": 15,
        "sport": {
          "id": 41
        }
      }
    ],
    "img": "images/icons/icon_mlb.svg"
  },
  ...

I am using LODASH to iterate over arrays. But I don't have an idea of how I can accomplish this task

EDIT

Do not get confuse because I mentioned above LIVE BETTING, I mean, I want the same to happen with every sport in the array.

2 Answers 2

3

try:

// in view, replace
{{sport.leagues.length}}

// with
{{ total(sport) }}

//in controller, add scope method
$scope.total = function(sport) {
    return _.sum(_.pluck(sport.leagues, 'offeringsAvailable'));
}
Sign up to request clarification or add additional context in comments.

Comments

0

Suggest just looping over each sport when you receive data and add an offerings property that contains the sum

sports.forEach(function(sport){ 
    sport.offerings=sport.leagues.reduce(function(prev,curr){     
        return prev + (curr.offeringsAvailable||0);
    },0);
});

Then in the header use {{sport.offerings}}

Comments

Your Answer

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.