0

I have the following Angular code, see below.

What I'd like to achieve is - for each user - find out the "count" for each activity type.

What I'd like to achieve is:

John Thompson = 2 Deal
Bill Gates = 3 Deal
Billy Wellington = 2 Deal, 1 Meeting

The activity id's are the same in the activities object, and the $scope.activities.

Bit stuck here, any idea on how to either a) Write a function to get the COUNT or b) Do something clever in the HTML element with a ng-repeat and group or something.

$scope.competitionDetails = {
        end_date: "2014-03-01",
        users: [{
            id: 2,
            name: "John Thompson",
            activities: [{ //This is all their activities
                id: 6431,
                time: (57).minutes().ago(),
                points: 20
            }, {
                id: 6431,
                time: new Date(),
                points: 20
            }]
        }, {
            id: 3,
            name: "Bill Gates",
            activities: [{ //This is all their activities
                id: 6431,
                time: new Date(),
                points: 20
            }, {
                id: 6431,
                time: new Date(),
                points: 20
            }, {
                id: 6431,
                time: new Date(),
                points: 20
            }]
        }, {
            id: 1,
            name: "Billy Wellington",
            activities: [{ //This is all their activities
                id: 6431,
                time: new Date(),
                points: 20
            }, {
                id: 6431,
                time: new Date(),
                points: 20
            }, {
                id: 6432,
                time: new Date(),
                points: 100
            }]
        }]
    };

    $scope.activities = [{
        id: 6431,
        name: "Deal",
        points: 20
    }, {
        id: 6432,
        name: "Meeting",
        points: 100
    }];

1 Answer 1

1

I have created a fiddle for you with a working example: http://jsfiddle.net/ADukg/4757/

Just using some regular js, with a tiny bit of angular:

// create a lookup    
var lookup = {};
angular.forEach($scope.activities, function(activity){
    lookup[activity.id] = activity;
});

// calculate the results
angular.forEach($scope.competitionDetails.users, function(user){
    user.results = {};
    angular.forEach(user.activities, function(activity){
        var name = lookup[activity.id].name;
        if(!user.results[name]){
            user.results[name] = 1;
        }else{                
            user.results[name] += 1;            
        }
    });        
});
Sign up to request clarification or add additional context in comments.

3 Comments

Damn that's perfect. Thanks @Matt Way!
If I wanted to only pull the results for one user, could I just create a function that passes in the userId and doesnt do a loop, but a look up or? How would you best approach that?
Yeah, just break apart my answer into a function that you could plug a single user into, and run it whenever you need a result.

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.