Javascript nested array transformation
<p>I'm trying to get my head around Javascript array functions.  I have a nested array like this, where every row covers the same time period:</p>

<pre><code>[{
    "category": "fruit",
    "variety": "apple",
    "data": [{
        "day": 1,
        "units": 2
    }, {"day": 2,
        "units": 4
    }]
},{
    "category": "fruit",
    "variety": "orange",
    "data": [{
        "day": 1,
        "units": 3
    }, {"day": 2,
        "units": 5
    }]
},{
    "category": "veg",
    "variety": "tomato",
    "data": [{
        "day": 1,
        "units": 4
    }, {"day": 2,
        "units": 2
    }]
}]
</code></pre>

<p>I would like to sum the units by day by category, to get an array like this:</p>

<pre><code>[{
    "category": "fruit",
    "data": [{
        "day": 1,
        "units": 5
    }, {"day": 2,
        "units": 9
    }]
},{
    "category": "veg",
    "data": [{
        "day": 1,
        "units": 4
    }, {"day": 2,
        "units": 2
    }]
}]
</code></pre>

<p>I've been tackling this through long looping if statements, and making a bit of a hash of it.  Can you see an elegant way to solve this?</p>

<p>Many thanks!</p>
