javascript - create json like key and value -


var data1=[     {"year":2000,      "country":"madagascar",      "country_id":847,      "indicator":"current account balance - national currency (millions)"     },     {"year":2005,      "country":"madagascar",      "country_id":847,      "indicator":"current account balance - national currency (millions)"     },     {"year":2000,      "country":"madagascar",      "country_id":847,      "indicator":"net income - national currency (millions)"     },     {"year":2005,      "country":"madagascar",      "country_id":847,      "indicator":"net income - national currency (millions)"     } ] 

this json data want using underscorejs

{   "key":"madagascar"   "values":[     {"year":2000,      "country":"madagascar",      "country_id":847,      "indicator":"current account balance - national currency (millions)"     },     {"year":2005,      "country":"madagascar",      "country_id":847,      "indicator":"current account balance - national currency (millions)"     }   ] } 

please can 1 can me solve problem using underscore.js

underscore has _.groupby() method can heavy lifting, creates object key each value:

var groupeddata1 = _.groupby(data1, 'country'); 

here's value of groupeddata1:

{"madagascar": [     {         "year": 2000,         "country": "madagascar",         "country_id": 847,         "indicator": "current account balance - national currency (millions)"     },      {         "year": 2005,         "country": "madagascar",         "country_id": 847,         "indicator": "current account balance - national currency (millions)"     },      {         "year": 2000,         "country": "madagascar",         "country_id": 847,         "indicator": "net income - national currency (millions)"     },      {         "year": 2005,         "country": "madagascar",         "country_id": 847,         "indicator": "net income - national currency (millions)"     } ]} 

to convert array of key/values, can use _.each() , _.keys() iterate through each country:

var groupeddata2 = [];  _.each(_.keys(groupeddata1), function (country) {     groupeddata2.push({         "key": country,          "values": groupeddata1[country]     }); }); 

this creates array of objects, 1 object each country:

[{     "key": "madagascar",     "values": [{         "year": 2000,         "country": "madagascar",         "country_id": 847,         "indicator": "current account balance - national currency (millions)"     }, {         "year": 2005,         "country": "madagascar",         "country_id": 847,         "indicator": "current account balance - national currency (millions)"     }, {         "year": 2000,         "country": "madagascar",         "country_id": 847,         "indicator": "net income - national currency (millions)"     }, {         "year": 2005,         "country": "madagascar",         "country_id": 847,         "indicator": "net income - national currency (millions)"     }] }]  

Comments

Popular posts from this blog

Hatching array of circles in AutoCAD using c# -

ios - UITEXTFIELD InputView Uipicker not working in swift -

Python Pig Latin Translator -