JS implementation of the JSON data row to column conversion of the instance code

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:

Copy Code code as follows:


function Covertdata (Jsondata,idfield, Fromfield, Tofield) {


var result = [], currecord =null, num;


var fromfields = fromfield.split (', ');


//Loop whole array: [{...},{...},{...},...]


for (Var idx=0;idx<jsondata.length;idx++) {


num = findidx (result, IDfield, Jsondata[idx][idfield]);


if (num!=-1) {


Currecord = Result[num];


}


else{


Currecord = {};


};


//loops The fields in each JSON object


for (var key in Jsondata[idx]) {


//processing of 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 data content, only processing 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++) {
if (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 ', 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 (' month1[', ' DFSOFT-PPQ ']);
Assertequals (' o ', 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 (' A ', 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 ']);
}
})


The test passed, indicating that the conversion was successful.

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.