0

I have called Git commit api, which gives array of 7 days which starts from Sunday, i want day & date from that array. eg. Sun 6 Aug Mon 7 Aug Tue 8 Aug .... I try to get this with following code which is suggested by 1 developer, but it gives only start day & date. Pls help me to achieve it.

Suggested code:

var data = data.json().data;
        if (data) {

            var months = {};
            for (var i = 0; i < data.length; i++) {
                console.log(data[i]);
                var monthNum = 0;
                if (data[i].week) {
                    console.log(new Date(data[i].week * 1000));
                    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);
        }

Api Response

    {  
   "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
         ]
    }

1 Answers1

1

Is this the output you want? Run the code and see if this helps.

function getComitDates() {
    var data = data.json().data;
    if(data) {
        var weekFlat = data.map(function(m){
                   return m.days.map(function(n,k){
                       return {
                           week:m.week,
                           dayIndex:k,
                           sum:n
                       }
                   })
                });
        var options = {month: 'short', day: 'numeric'};
        var result = weekFlat.map(function(arr){
            return arr.map(function(ed){ 
                var date = ed.week?new Date((ed.week*1000) + ed.dayIndex*24*3600*1000) : new Date(); 
                return date.toLocaleDateString('en-US', options) + ' ' +ed.sum
            });
        }).join(",").split(",");
        console.log(result);
    }
}
Rajesh Dan
  • 459
  • 7
  • 16
  • 1
    Just add an filter to the final output `result` `var result = weekFlat.map(function(arr){ return arr.map(function(ed){ var date = ed.week?new Date((ed.week*1000) + ed.dayIndex*24*3600*1000) : new Date(); return {date:date,comit:ed.sum} }).filter(function(x){ return x.date.getMonth() == 7 && x.date.getDate() == 18 }); }).filter(function(x){return x.length});` – Rajesh Dan Aug 10 '17 at 09:03
  • I think i not able to explain my requirement, If i want to show only Months, Date & no commits. eg. 6 Aug 1, 8 Aug 2 , 9 Aug 2...like this from the array want to show only month, date & commits – Vishal Freelancer Aug 10 '17 at 09:16
  • Pls suggest on this : - How can i get current date from array & 5 days earlier. eg. if today is Aug 10 it should be Aug 5, Aug 6, Aug 7, Aug 8, Aug 9, Aug 10 & no of commits of each date – Vishal Freelancer Aug 11 '17 at 04:16
  • 1
    @VishaalM more conditions means more filter, you can easily filter your dates for within last `n` days, either using `moment.js` or just calculating date values in js, see here https://stackoverflow.com/a/22851003/2293313 – Rajesh Dan Aug 11 '17 at 09:40