Wednesday, 21 August 2013

create javascript object (with key pairs) from json (not in correct key pair)

create javascript object (with key pairs) from json (not in correct key pair)

I don't know how to extract key/values from a json array to hook them into
different parts of a javascript array.
E.g. I have 2 json arrays which looks like this:
series 1:
{"label1":"91970","label2":"1231", ... "labeln": "somenumbers".}
series 2:
{"label1":"4556","label2":"3434", ....}
I would like to transform both of them to 1 javascript object (for nvd3
plotting) like this:
long_short_data = [
{
key: 'Series1',
color: '#d62728',
values: [
{
"cell" : "label1" ,
"value" : 91970
} ,
{
"cell" : "label2" ,
"value" : 1231
} ,
.....
]
},
{
key: 'Series2',
color: '#1f77b4',
values: [
{
"cell" : "label1" ,
"value" : 4556
} ,
{
"cell" : "label2" ,
"value" : 3434
} ,
...
}
]
}
];
Appreciate your help.

No comments:

Post a Comment