Divide 2 values taken from the Json with AngularJS

0

I have another problem that I do not know how to do it.

I have a Json with several data and I have to make an average between 2 json data

angular.module('playersApp', []).controller("AllPlayerssCtrl", function ($scope) {
$scope.players = [
    {     
        "stats": [
          {
            "name": "goals",
            "value": 5
          },
          {
            "name": "losses",
            "value": 20
          },
          {
            "name": "wins",
            "value": 48
          },
          {
            "name": "draws",
            "value": 23
          },
          {
            "name": "fwd_pass",
            "value": 1533
          },
          {
            "name": "goal_assist",
            "value": 2
          },
          {
            "name": "appearances",
            "value": 80
          },
          {
            "name": "mins_played",
            "value": 6953
          },
          {
            "name": "backward_pass",
            "value": 308
          }
        ]
      },...];

I have to show the average number of goals per game by window, that is, divide the value of goals between the value of mins_played and I do not know how to do the function by taking those values from the json and from there in the ng-if of the html call that function to show the result.

If you can help me please, I would appreciate it.

Thank you very much

    
asked by derek 22.08.2017 в 01:13
source

1 answer

2

function MyController($scope) {

  $scope.players = [{
      "stats": [{
          "name": "goals",
          "value": 5
        },
        {
          "name": "losses",
          "value": 20
        },
        {
          "name": "wins",
          "value": 48
        },
        {
          "name": "draws",
          "value": 23
        },
        {
          "name": "fwd_pass",
          "value": 1533
        },
        {
          "name": "goal_assist",
          "value": 2
        },
        {
          "name": "appearances",
          "value": 80
        },
        {
          "name": "mins_played",
          "value": 6953
        },
        {
          "name": "backward_pass",
          "value": 308
        }
      ]
    },

    
  ];
  for (var i = 0; i < $scope.players.length; i++) {
    for (var x=0; x< $scope.players[i].stats.length; x++) {
      switch($scope.players[i].stats[x].name) {
        case 'goals':
         var tmp_goals = $scope.players[i].stats[x].value;
        break;
        case 'mins_played':
         var tmp_mins = $scope.players[i].stats[x].value;
        break;
        
      }
      
    }
    
    $scope.players[i].media = tmp_goals/tmp_mins;


  }


}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.1/angular.min.js"></script>
<div ng-app ng-controller="MyController">

  <div ng-repeat="jugador in players">

    <p>
      <p>
        {{(jugador.media) | number:4}}

  </div>

</div>

There are many ways to do it, some more elegant than others, I would use this one, in which I take the data that interests me from the array, the process, and I put the result in a new key.

If tomorrow you need to show some more data of the statistic, for example attendances per minute played, you put in the goal_assist switch, do the calculation on $scope.players[i].media_asistencia = tmp_goals_assist/tmp_mins and then paint per screen jugador.media_asistencia

    
answered by 22.08.2017 / 09:54
source