Q:

Better approach to converting two JSON structures

I have the following JSON:
{ 
  "Foo": [{"total": 2000, year: 1}, {"total": 3400, year: 2}, {"total": 5000, year: 3}],
  "Bar": [{"total": 1000, year: 1}, {"total": 2400, year: 2}, {"total": 7000, year: 3}],
}

I need it in the form:
[
  { "Foo": 2000, "Bar": 1000, year: 1},
  { "Foo": 3400, "Bar": 2400, year: 2},
  { "Foo": 5000, "Bar": 7000, year: 3}
]

The best we can come up with is a quite frankly embarassing:
var yearMap = {};

// Initialising the map of years eg. {1: { } , 2: {} .. }
for (var key in data) {
    var foos = data[key];
    $(foos).each(function(index, val) {
        yearMap[val.year] = { };
    });
}

// Iterates through the data and just puts the commission total against the year
for (var key in data) {
    var foos = data[key];
    $(foos).each(function(index, val) {
        yearMap[val.year][key] = val.total.value;
    });
}

Can anyone come up with a cleaner approach?

A:

I wouldn't call it cleaner(basically it's doing the same ), but it iterates only 1 time over the entire data:
var yearMap = {};

$.each(data,function(k,v){
  $.each(v,function(kk,vv){
    if(!yearMap[vv.year])yearMap[vv.year]={};
    yearMap[vv.year][k]=vv.total
  });
});

