月份 郑州-电量 新乡-电量 安阳-电量
201201 33 29 23
201202 35 26 25
201203 34 27 24
201204 36 28 26
201205 34.3 28.8 24.3
这样,Ext的图表就能把它显示成三个序列了。
我写了如下的函数实现这个功能:
复制代码 代码如下:
function CovertData(jsonData,idField, fromField, toField){
var result = [], curRecord =null, num;
var fromFields = fromField.split(',');
// 循环整个数组:[{...},{...},{...},...]
for(var idx=0;idx<jsonData.length;idx++){
num = findIdx(result, idField, jsonData[idx][idField]);
if(num!=-1){
curRecord = result[num];
}
else{
curRecord = {};
};
// 循环每个json对象中的字段
for(var key in jsonData[idx]){
// 处理转换的数据内容
for(var i=0;i<fromFields.length;i++){
if(key == fromFields[i]){
curRecord[jsonData[idx][toField]+'-' + fromFields[i]] = jsonData[idx][key];
break;
}
}
// 除数据内容外,只处理标识字段数据
if(key == idField){
curRecord[key] = jsonData[idx][key];
}
}
if(num==-1){
result.push(curRecord);
}
}
return result;
}

function findIdx(jsonData, columnName, value){
for(var idx = 0;idx<jsonData.length;idx++){
if(jsonData[idx][columnName]==value)
return idx;
}
return -1;
}
JsTestDriver的测试代码如下:
TestCase("Test json data row to column",{
setUp:function(){
this.jsonData = [{yearmonth:201201,ppq:23,spq:27,company:'dfsoft'},
{yearmonth:201202,ppq:33,spq:38,company:'dfsoft'},
{yearmonth:201203,ppq:43,spq:49,company:'dfsoft'},
{yearmonth:201204,ppq:53,spq:51,company:'dfsoft'},
{yearmonth:201201,ppq:29,spq:26,company:'vcom'},
{yearmonth:201202,ppq:34,spq:38,company:'vcom'},
{yearmonth:201203,ppq:48,spq:43,company:'vcom'},
{yearmonth:201204,ppq:52,spq:59,company:'vcom'}];

var fromField = 'ppq,spq', toField = 'company', idField = 'yearmonth';
this.resultData = CovertData(this.jsonData,idField,fromField, toField);
},
"test store has columns":function(){
var month1 = this.resultData[findIdx(this.resultData,'yearmonth',201201)];
var month2 = this.resultData[findIdx(this.resultData,'yearmonth',201202)];
var month3 = this.resultData[findIdx(this.resultData,'yearmonth',201203)];
var month4 = this.resultData[findIdx(this.resultData,'yearmonth',201204)];

assertEquals(4,this.resultData.length);
assertEquals('23',month1['dfsoft-ppq']);
assertEquals('29',month1['vcom-ppq']);
assertEquals('33',month2['dfsoft-ppq']);
assertEquals('34',month2['vcom-ppq']);
assertEquals('43',month3['dfsoft-ppq']);
assertEquals('48',month3['vcom-ppq']);
assertEquals('53',month4['dfsoft-ppq']);
assertEquals('52',month4['vcom-ppq']);

assertEquals('27',month1['dfsoft-spq']);
assertEquals('26',month1['vcom-spq']);
assertEquals('38',month2['dfsoft-spq']);
assertEquals('38',month2['vcom-spq']);
assertEquals('49',month3['dfsoft-spq']);
assertEquals('43',month3['vcom-spq']);
assertEquals('51',month4['dfsoft-spq']);
assertEquals('59',month4['vcom-spq']);
}
})

测试通过,表示转换成功。

标签:
json,数据,转换

免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
狼山资源网 Copyright www.pvsay.com

评论“js实现json数据行到列的转换的实例代码”

暂无“js实现json数据行到列的转换的实例代码”评论...

RTX 5090要首发 性能要翻倍!三星展示GDDR7显存

三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。

首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。

据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。