JSFiddle - React, Tailwind, and code Playground

by Avi Algaly

JavaScript

var distList = [{yearId: 2014,monthId: 1,RecCount: 1},{yearId: 2014,monthId: 2,RecCount: 1},{yearId: 2014,monthId: 3,RecCount: 1},{yearId: 2014,monthId: 4,RecCount: 1},{yearId: 2014,monthId: 5,RecCount: 1},{yearId: 2014,monthId: 10,RecCount: 1},{yearId: 2014,monthId: 12,RecCount: 1},{yearId: 2015,monthId: 11,RecCount: 1}];

var getClosestMonthToDisplay = function (distList) {
     var month = new Date().getUTCMonth();
     var year = new Date().getUTCFullYear();
     if (distList.length === 0) return false;
     var closestYear = 0;
     var closestMonth = 0;
     for (var i = 0; i < distList.length; i++) {
         if (closestYear <= distList[i].year) closestYear = distList[i].year;
     }
     for (var i = 0; i < distList.length; i++) {
         if (closestYear == distList[i].year) if (closestMonth <= distList[i].month) closestMonth = distList[i].month
     }
    console.log(closestYear,":",closestMonth)
 }

getClosestMonthToDisplay(distList)