字符串/日期替换基于jquery中的if条件

时间:2015-03-22 06:05:08

标签: javascript jquery arrays if-statement for-loop

我正在尝试将错过的月份替换为错过的月份

我有一个数据,我正在创建数组DATE,MARKETS。在数据中,有几个月缺失。我写了下面的代码来取代错过的月份。为此我写了下面的代码 这是FIDDLE

var data = [
{ "MFG_NAME": "ABC", "CONCATED_MKT_SHARE": "01-SEP-14|0.59" },
{ "MFG_NAME": "XYZ", "CONCATED_MKT_SHARE": "01-MAY-14|0.87" },
{ "MFG_NAME": "ABC", "CONCATED_MKT_SHARE": "01-NOV-14|0.25" },
{ "MFG_NAME": "XYZ", "CONCATED_MKT_SHARE": "01-JUL-14|0.67" },
{ "MFG_NAME": "ABC", "CONCATED_MKT_SHARE": "01-DEC-14|0.10" },
{ "MFG_NAME": "XYZ", "CONCATED_MKT_SHARE": "01-OCT-14|0.03" },
{ "MFG_NAME": "XYZ", "CONCATED_MKT_SHARE": "01-DEC-14|0.14" }];
var monthNames = ["JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"];
var ctdate = (new Date()).getMonth() + 1;// getting current month
var dynmonths = new Array();
dynmonths = monthNames.slice(ctdate).concat(monthNames.slice(0, ctdate));//creating array of months starting from current month
var DATES = [[], []];
var MARKETS = [[], []];
var mfg;
function findDataForMonth(mfgName, month) {
  for (var i = 0, length = data.length; i < length; i++) {
    var datum = data[i];
    if (datum.MFG_NAME === mfgName && datum.CONCATED_MKT_SHARE.indexOf(month) >= 0) {
        return datum.CONCATED_MKT_SHARE.split('|')[1];
      }
     }
   }

for (var i = 0, length = dynmonths.length; i < length; i++) {
// what is the if condition that has to be written here  
var month = '01-' + dynmonths[i] + '-14';
alert(month);   //the output here is 01-APR-14, 01-MAY-14, 01-JUN-14, 01-JUL-14, 01-AUG-14, 01-SEP-14, 01-OCT-14, 01-NOV-14, 01-DEC-14, `01-JAN-14, 01-FEB-14, 01-MAR-14`

 //Here after 01-DEC-14 the next must be 01-JAN-15 instead it is 01-JAN-14.  How do I get it?

MARKETS[0].push(findDataForMonth("ABC", month) || 0.0);
MARKETS[1].push(findDataForMonth("XYZ", month) || 0.0);

DATES[0].push(month);
DATES[1].push(month);
 }

提前致谢

0 个答案:

没有答案