Vishal Freelancer
Vishal Freelancer

Reputation: 95

Github APi for no of commits

I have called a git-hub no of commits api, it gives the data week wise for a year, i want to sort it as month wise. Is there any alternative way to get no of commits in a month. How would i achieve it. Pls help to sort it out. Received following data on api call, i want to sort it month wise & then want to show no.of commits in a month.

    {  
   "success":true,
   "data":[  
      {  
         "days":[  
            0,
            0,
            0,
            0,
            0,
            0,
            0
         ],
         "total":0,
         "week":1471132800
      },
      {  
         "days":[  
            0,
            0,
            0,
            0,
            0,
            0,
            0
         ],
         "total":0,
         "week":1471737600
      },
      {  
         "days":[  
            0,
            0,
            2,
            4,
            0,
            0,
            0
         ],
         "total":6,
         "week":1472342400
      },
      {  
         "days":[  
            0,
            0,
            0,
            0,
            0,
            0,
            0
         ],
         "total":0,
         "week":1472947200
      },
      {  
         "days":[  
            0,
            0,
            0,
            0,
            0,
            0,
            0
         ],
         "total":0,
         "week":1473552000
      },
      {  
         "days":[  
            0,
            0,
            0,
            0,
            0,
            0,
            0
         ],
         "total":0,
         "week":1474156800
      },
      {  
         "days":[  
            0,
            0,
            0,
            0,
            0,
            0,
            0
         ],
         "total":0,
         "week":1474761600
      },
      {  
         "days":[  
            0,
            0,
            0,
            0,
            0,
            0,
            0
         ],
         "total":0,
         "week":1475366400
      },
      {  
         "days":[  
            0,
            0,
            0,
            0,
            0,
            0,
            0
         ]
    }

Upvotes: 0

Views: 57

Answers (1)

Brian
Brian

Reputation: 5059

This should help get you started:

var data = {}; // the data you have from Github

var months = {};

for (var i = 0; i < data.length; i++) {
  console.log(data[i]);

  // If we have a week, use it. If we don't, it looks like it's the current week?
  var monthNum = 0;
  if (data[i].week) {
    monthNum = new Date(data[i].week * 1000).getMonth();  
  } else {
    monthNum = new Date().getMonth()
  }

  if (!months[monthNum]) {
    months[monthNum] = [];
  }

  months[monthNum].push(data[i]);
}

console.log(months);

Upvotes: 1

Related Questions