1. #1
    Sencha User
    Join Date
    Oct 2012
    Posts
    25
    Vote Rating
    0
    skylinezpromise is on a distinguished road

      0  

    Default How to count json items for each param ?

    How to count json items for each param ?


    I have a JSON store :

    Code:
     {
            "Week": 1145,
            "Sev_Logged": 22,
            "From": "IN1"
        },
        {
            "Week": 1145,
            "Sev_Logged": "44",
            "From": "IN1"
        },
        {
            "Week": 1145,
            "Sev_Logged": 54,
            "From": "IN2"
        },
        {
            "Week": 1146,
            "Sev_Logged": "66",
            "From": "IN1"
        },
        {
            "Week": 1146,
            "Sev_Logged": "66",
            "From": "IN2"
        }
    I want to count for each "Week", the number of "From", for example for Week 1146, i'd get IN1 = 1 and IN2 = 1 and for week 1145 i'd get IN1 = 2 and IN2 = 1.

    I've written a function that loops through my data store to count IN1 and IN2 for each param:

    Code:
    countBy: function(param){
            var count = {};
            this.each(function(item){
                var type = item.get(param);
                if (Ext.isDefined(count[type])){
                    count[type]++;
                } else {
                    count[type] = 1;
                }                               
            });
            return count;
        }
    But the problem is that when I give it "Week" in param, it doesn't count IN1 and IN2 for each WEEK, it returns "1145" : 3 and 1146 : 2, but what i want is : "1145" : {IN1 : 2} and "1146" : {IN1 : 1}.

    Thank you for your help!

  2. #2
    Sencha - Community Support Team
    Join Date
    Jan 2012
    Posts
    1,376
    Vote Rating
    115
    vietits is a splendid one to behold vietits is a splendid one to behold vietits is a splendid one to behold vietits is a splendid one to behold vietits is a splendid one to behold vietits is a splendid one to behold

      0  

    Default


    Code:
    countBy: function(param){
        var count = {};
        this.each(function(item){
            var type = item.get(param);
            var from = item.get('from');
            count[type] = count[type] || {};
            if (Ext.isDefined(count[type][from])){
                count[type][from]++;
            } else {
                count[type][from] = 1;
            }                               
        });
        return count;
    }

Thread Participants: 1

Tags for this Thread