I have some JSON data that looks like this, where each object has three properties:
data = [
{
"country" : "Ireland",
"year" : "2013",
"number" : 45
},
{
"country" : "Ireland",
"year" : "2014",
"number" : 23430
},
{
"country" : "Honduras",
"year" : "2013",
"number" : 1582
},
{
"country" : "Honduras",
"year" : "2014",
"number" : 3458
}
]
I want to transform my data so that there are just two properties per object. The property named "country" would remain the same. I want to combine the other two properties, so that the value of "year" becomes the key of the new property, and the value of "number" is the value of the new property. So the new JSON array of objects would look like this:
newData = [
{
"country" : "Ireland",
"2013" : 45,
"2014" : 23430
},
{
"country" : "Honduras",
"2013" : 1582,
"2014" : 3458
}
]
How would I go about doing that? I've done a lot of searching and can't find a solution. I'd prefer to just use javascript to do this and not a library.