0

Not sure exactly how to explain it but what i am trying to do is return the amount of times "Jack" is the string attached to the winner property in my array.

     function myCtrl($scope) {
       $scope.data = [{
         game: 1,
         dnscore: 10,
         bwscore: 9,
         winner: "Jack"
       }, {
         game: 2,
         dnscore: 9,
         bwscore: 10,
         winner: "Jill"
       }, {
         game: 3,
         dnscore: 9,
         bwscore: 10,
         winner: "Jill"
       }, {
         game: 4,
         dnscore: 6,
         bwscore: 10,
         winner: "Jill"
       }];
     };

Then be able to bind {{jackwins}} into my document. Any and all help is appreciated.

0

3 Answers 3

1
$scope.jackWinCount = function() {
    var result = 0;
    for (var i = 0; i < $scope.data.length; i++) {
        if ($scope.data[i].winner == "Jack") {
            result++;
        }
    }
    return result;
});

and in your template:

{{ jackWinCount() }}
Sign up to request clarification or add additional context in comments.

Comments

1

You may do it this way:

Change your data array with function add:

function myCtrl($scope) {
    $scope.data = [];
    $scope.add = function(dt){
        data.push(dt);
        if(dt.winner == "Jack")
            $scope.jackwins++;
    }
    $scope.jackwins = 0;
};

UPD. Or you may do it via function - this way is more flexible

function myCtrl($scope) {
    $scope.data = [
        {winner:"Jack", ...},
        {winner:"Jimm", ...}
    ];
    $scope.getWinsCount = function(name){
        var c = 0;
        for(var i=0;i<$scope.data.length;i++){
            if(name==$scope.data[i].winner)
                c++;
        }
        return c;
    };
};

Than you is your html you call it like this <div>{{getWinsCount('Jack')}}</div>

Comments

0

If u want to filter data in angular you can used filter on array to filter the data

Below is the example:

$scop.newFilterData=$filter("filter")($scope.data , {"winner": "Jill"});
{{$scop.newFilterData.length}}

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.