这是我的json_encode输出
$result = '[{ MFG_NAME: "ABC", CONCATED_MKT_SHARE: "01-OCT-14|0.59" },
{ MFG_NAME: "XYZ", CONCATED_MKT_SHARE: "01-OCT-14|0.87" },
{ MFG_NAME: "ABC", CONCATED_MKT_SHARE: "01-NOV-14|0.25" },
{ MFG_NAME: "XYZ", CONCATED_MKT_SHARE: "01-NOV-14|0.67" },
{ MFG_NAME: "ABC", CONCATED_MKT_SHARE: "01-DEC-14|0.10" },
{ MFG_NAME: "XYZ", CONCATED_MKT_SHARE: "01-DEC-14|0.03" }]';
这是我在jquery中所做的
$(function () {
var data = new Array();
data = <?php echo $result; ?>;
var MFG_NAME = [];
var DATE = [];
var MKT = [];
data.forEach(function(item) {
var parts = item.CONCATED_MKT_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());
}
});
});
ABC和XYZ都缺少一些月份。有没有办法用错过的月份替换丢失的月份,这样我就可以获得所有12个月以及12个mktshare值,用0替换连接值
答案 0 :(得分:0)
您可以执行类似
的操作$(function () {
var months = ['JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC']
var data = new Array();
data = JSON.parse('[{"MFG_NAME":"ABC","CONCATED_MKT_SHARE":"01-MAR-14|0.74,01-APR-14|0.58,01-MAY-14|0.21,01-JUN-14|0.22,01-JUL-14|0.24,01-SEP-14|0.96,01-DEC-14|0.21,01-AUG-14|0.34"},{"MFG_NAME":"XYZ","CONCATED_MKT_SHARE":"01-MAR-14|0.44,01-MAY-14|0.82,01-JUN-14|0.71,01-AUG-14|0.22,01-JUL-14|0.24,01-SEP-14|0.96,01-DEC-14|0.21,01-AUG-14|0.34"}]');
var x = 0;
for (var i = 0; i < data.length; i++) {
var mfg = data[i],
share = mfg.CONCATED_MKT_SHARE;
for (var j = 0; j < months.length; j++) {
if (share.indexOf('-' + months[j] + '-') == -1) {
share += ',01-' + months[j] + '-14|0.0';
}
}
}
var dlen = data.length;
var cmname = new Array();
var cmdate = new Array();
var crank = [];
var mktshare = new Array();
for (i = 0; i < dlen; i++) {
cmname[i] = data[i].MFG_NAME;
shar = new Array();
shar = data[i].CONCATED_MKT_SHARE.split(',');
shlen = shar.length;
shrk = new Array();
shard = new Array();
for (j = 0; j < shlen; j++) {
shaak = new Array();
shaak = shar[j].split('|');
shrk[j] = shaak[0];
shard[j] = parseFloat(shaak[1]);
}
cmdate[i] = shrk;
mktshare[i] = shard;
}
});
演示:Fiddle