samedi 21 mars 2015

String / Date replacement based on if condition in jquery

I am trying to replace the missing months with the month that is missed


I have a data from which I am creating arrays DATE, MARKETS. In the data, there are few months that are missing. I have written the below code to replace the missed month. For that I have written the below code



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);
}


Thanks in advance


Aucun commentaire:

Enregistrer un commentaire