这是我的输出
[{ "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" }]
在此ABC
中,只有3个月SEP, NOV, DEC
和XYZ
的数据包含4个月MAY, JUL, OCT, DEC
的数据。我试图从此输出中获取数据。但是由于这几个月我在实施它时遇到了问题。 ABC
只有3个月,而其他9个月则缺失。同样在XYZ
的情况下有4个月,而其他8个月则缺失。我试图用这个月替换这些缺失的月份,并将相应的值替换为0.0。
例如在ABC
我尝试用01-OCT-14|0.0
代替所有病例的月份之间缺少OCT。为此我写了下面的代码,但它不起作用。请更正代码。
var data = new Array();
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 currentTime = new Date();//Sat Mar 21 2015 17:19:15 GMT+0530
var monthNames = ["JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"];
var ctdate = (new Date()).getMonth() + 1; //Here I get the current month eg:MAR
var dynmonths = new Array();
dynmonths = monthNames.slice(ctdate).concat(monthNames.slice(0, ctdate));//Here I get previous 12 months for comparision
//["APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC","JAN","FEB","MAR"];
var MFG_NAME = [];
var DATE = [];
var MKT = [];
//替换月份的代码从这里开始
data.forEach(function(item) {
var share = item.CONCATED_MKT_SHARE;
for (var j = 0; j < dynmonths.length; j++) {
if (share.indexOf('-' + dynmonths[j] + '-') == -1) {
share += ',01-' + dynmonths[j] + '-14|0.0';
}
}// The months are replaced at the end, instead I want to replace it in order of the month names
//这是我的实际实现代码,我得到输出数组DATE和MKT
var share = item.CONCATED_MKT_SHARE;
var parts = share.split("|");
var i = MFG_NAME.indexOf(item.MFG_NAME);
if (i == -1) {
MFG_NAME.push(item.MFG_NAME);
DATE.push([parts.shift()]);
MKT.push([+parts.shift()]);
}
else {
DATE[i].push(parts.shift());
MKT[i].push(+parts.shift());
}
});
我正在接受DATE和MKT,但我没有得到失踪的月份
预期输出
DATE[0] = ["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"];
DATE[1] = ["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"];
MKT[0] = ["0.0","0.0","0.0","0.0","0.0","0.59","0.0","0.25","0.10","0.0","0.0","0.0"];
MKT[1] = [""0.0","0.75","0.0","0.67","0.0","0.0","0.03","0.0","0.14"]
答案 0 :(得分:0)
我已经创建了一个解决方案,你可以试试这个
var i=0;
for(var jo in pdata)
{
DATE.push([]);
MKT.push([]);
for(var mnt in monthNames)
{
var index = pdata[jo].m.indexOf("01-"+monthNames[mnt]+"-14");
if(index<0)
{
DATE[i].push("01-"+monthNames[mnt]+"-14");
MKT[i].push("0.0");
}
else{
DATE[i].push(pdata[jo].m[index]);
MKT[i].push(pdata[jo].v[index]);
}
}
i++;
}
var element = "";
for(i=0;i<DATE.length;i++)
{
element += DATE[i].join(", ");
element += "<br/>";
element += MKT[i].join(", ");
element += "<br/>";
}
请检查小提琴here
答案 1 :(得分:0)
当@Learning发布他的答案时,我已经开始了,所以我刚刚完成了另一次拍摄。
http://jsfiddle.net/bvaughn/ah4c0oah/3/
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 DATES = [[], []];
var MARKETS = [[], []];
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];
}
}
}
var monthsToCheck = [];
function addMonthsForYear(year, minMonth, maxMonth) {
for (var i = 0, length = monthNames.length; i < length; i++) {
if ((!minMonth || i >= minMonth) && (!maxMonth || i <= maxMonth)) {
monthsToCheck.push('01-' + monthNames[i] + '-' + year);
}
}
}
var monthOffset = new Date().getMonth();
addMonthsForYear(12, monthOffset);
addMonthsForYear(13);
addMonthsForYear(14);
addMonthsForYear(15, undefined, monthOffset);
for (var i = 0, length = monthsToCheck.length; i < length; i++) {
var month = monthsToCheck[i];
MARKETS[0].push(findDataForMonth("ABC", month) || 0.0);
MARKETS[1].push(findDataForMonth("XYZ", month) || 0.0);
DATES[0].push(month);
DATES[1].push(month);
}
console.log(DATES);
console.log(MARKETS);