JS implementation of JSON data row to column conversion

Source: Internet
Author: User
Tags json

In order to achieve such data display three sequence, Zhengzhou, Xinxiang, Anyang electricity, you need to realize the conversion of such data, converted into the following form:

Zhengzhou-Electric power Xinxiang-electricity-electricity

201201 33 29 23

201202 35 26 25

201203 34 27 24

201204 36 28 26

201205 34.3 28.8 24.3

This way, the ext chart will show it as a three sequence.

I wrote the following function to implement this function:

Function Covertdata (Jsondata,idfield, Fromfield, Tofield) {
var result = [], currecord =null, num;
var fromfields = fromfield.split (', ');
//Loop the entire array: [{...}},{...},{},...]
for (Var idx=0;idx<jsondata.length;idx++) {
num = findidx (result, IDfield, Jsondata[idx][idfield));
if (num!=-1) {
Currecord = Result[num];
}
else{
Currecord = {};
};
//loop fields in each JSON object
for (var key in Jsondata[idx)) {
//process transformed data content
for (var i=0;i<fromfields.length;i++) {
if (key = = Fromfields[i]) {
currecord[jsondata[idx][tofield]+ '-' + fromfields[i]] = Jsondata[idx][key];
break;
}
}
///Except for data content, only process identity field data
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++) {
I F (Jsondata[idx][columnname]==value)
return idx;
}
Return-1; The test code for the
}
Jstestdriver is as follows:

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 ', 2 01201)];
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 (' month1[', ' DFSOFT-PPQ ']);
Assertequals (' month1[', ' VCOM-PPQ ']);
Assertequals (' month2[', ' DFSOFT-PPQ ']);
Assertequals (' month2[', ' VCOM-PPQ ']);
Assertequals (' month3[', ' DFSOFT-PPQ ']);
Assertequals (' month3[', ' VCOM-PPQ ']);
Assertequals (' n ', month4[' DFSOFT-PPQ ']);
Assertequals (' month4[', ' VCOM-PPQ ']);

Assertequals (' month1[', ' DFSOFT-SPQ ']);
assertequals (' num ', month1[' VCOM-SPQ '));
Assertequals (' month2[', ' DFSOFT-SPQ ']);
Assertequals (' month2[', ' VCOM-SPQ ']);
Assertequals (' month3[', ' DFSOFT-SPQ ']);
Assertequals (' month3[', ' VCOM-SPQ ']);
Assertequals (' Wuyi ', month4[' DFSOFT-SPQ ']);
Assertequals (' month4[', ' VCOM-SPQ ']);
}
})
test passed to indicate successful conversion.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.