Combine Dates with Homerun Data from JSON
<p>The idea is to take "Dates" and "homeruns" from the .json. Combine matching Dates together and add up the homeruns. Currently I'm able to display the Dates combined together with matching dates but the Homeruns don't add up, just stack next to each other. For example .... Date: "1985", Hits: "3", Homeruns: "101"}
Eventually I want to all take the dates with combined homeruns and display it in highcharts in a barchart (but i'll figure that out later).</p>

<p><strong>chart.json</strong></p>

<pre><code>[{"Date": "1903", "Hits": "0", "Homeruns": "1"}, {"Date": "1903", "Hits": "1", "Homeruns": "2"}, {"Date": "1904", "Hits": "0", "Homeruns": "1"}, {"Date": "1904", "Hits": "1", "Homeruns": "2"}, {"Date": "1904", "Hits": "1", "Homeruns": "1"}, {"Date": "1905", "Hits": "1", "Homeruns": "0"}, {"Date": "1907", "Hits": "0", "Homeruns": "1"}, {"Date": "1907", "Hits": "0", "Homeruns": "1"}, {"Date": "1909", "Hits": "1", "Homeruns": "0"}, {"Date": "1909", "Hits": "0", "Homeruns": "1"}, {"Date": "1909", "Hits": "0", "Homeruns": "0"}, {"Date": "1909", "Hits": "0", "Homeruns": "1"}, {"Date": "1910", "Hits": "0", "Homeruns": "2"}, {"Date": "1910", "Hits": "0", "Homeruns": "0"}, {"Date": "1910", "Hits": "?", "Homeruns": "1"}, {"Date": "1911", "Hits": "0", "Homeruns": "1"}, {"Date": "1912", "Hits": "0", "Homeruns": "1"}, {"Date": "1914", "Hits": "0", "Homeruns": "1"}, {"Date": "1915", "Hits": "1", "Homeruns": "0"}, {"Date": "1916", "Hits": "1", "Homeruns": "0"}, {"Date": "1916", "Hits": "?", "Homeruns": "1"}, {"Date": "1917", "Hits": "1", "Homeruns": "0"}, {"Date": "1918", "Hits": "1", "Homeruns": "0"}, {"Date": "1918", "Hits": "0", "Homeruns": "1"}, {"Date": "1918", "Hits": "0", "Homeruns": "1"}, {"Date": "1918", "Hits": "0", "Homeruns": "1"}, {"Date": "1919", "Hits": "0", "Homeruns": "0"}, {"Date": "1919", "Hits": "1", "Homeruns": "0"}, {"Date": "1919", "Hits": "0", "Homeruns": "2"}, {"Date": "1920", "Hits": "1", "Homeruns": "0"}, {"Date": "1920", "Hits": "1", "Homeruns": "0"}, {"Date": "1920", "Hits": "0", "Homeruns": "0"}, {"Date": "1920", "Hits": "0", "Homeruns": "1"}, {"Date": "1920", "Hits": "1", "Homeruns": "0"}, {"Date": "1920", "Hits": "1", "Homeruns": "0"}, {"Date": "1920", "Hits": "0", "Homeruns": "1"}, {"Date": "1922", "Hits": "0", "Homeruns": "2"}, {"Date": "1922", "Hits": "0", "Homeruns": "1"}, {"Date": "1931", "Hits": "0", "Homeruns": "1"}, {"Date": "1934", "Hits": "0", "Homeruns": "1"}, {"Date": "1935", "Hits": "0", "Homeruns": "1"}, {"Date": "1936", "Hits": "0", "Homeruns": "2"}, {"Date": "1937", "Hits": "0", "Homeruns": "1"}, {"Date": "1937", "Hits": "2", "Homeruns": "0"}, {"Date": "1940", "Hits": "0", "Homeruns": "1"}, {"Date": "1942", "Hits": "0", "Homeruns": "1"}, {"Date": "1946", "Hits": "1", "Homeruns": "0"}, {"Date": "1948", "Hits": "0", "Homeruns": "1"}, {"Date": "1949", "Hits": "1", "Homeruns": "0"}, {"Date": "1949", "Hits": "0", "Homeruns": "1"}, {"Date": "1950", "Hits": "1", "Homeruns": "0"}, {"Date": "1951", "Hits": "0", "Homeruns": "1"}, {"Date": "1951", "Hits": "0", "Homeruns": "1"}, {"Date": "1951", "Hits": "0", "Homeruns": "1"}, {"Date": "1951", "Hits": "0", "Homeruns": "1"}, {"Date": "1952", "Hits": "0", "Homeruns": "1"}, {"Date": "1952", "Hits": "0", "Homeruns": "1"}, {"Date": "1954", "Hits": "0", "Homeruns": "1"}, {"Date": "1954", "Hits": "2", "Homeruns": "1"}, {"Date": "1955", "Hits": "0", "Homeruns": "1"}, {"Date": "1956", "Hits": "2", "Homeruns": "1"}]
</code></pre>

<p><strong>JS</strong></p>

<pre><code>function chartPage()
        {
            $.ajax(
            {
                url: 'chart.json',
                data:
                {},
                dataType: 'json',
                success: function(data)
                {
                    var stringy = JSON.stringify(data);
                    var objects = $.parseJSON(stringy);
                    var categories = new Array();
                    var groupedObjects = new Array();
                    var i = 0;
                    _.each(objects, function(obj)
                    {
                        var existingObj;
                        if ($.inArray(obj.Date, categories) &gt;= 0)
                        {
                            existingObj = _.find(objects, function(o)
                            {
                                return o.Date === obj.Date;
                            });
                            existingObj["Homeruns"] += obj["Homeruns"];
                        }
                        else
                        {
                            groupedObjects[i] = obj;
                            categories[i] = obj.Date;
                            i++;
                        }
                    });
                    groupedObjects = _.sortBy(groupedObjects, function(obj)
                    {
                        return obj["Homeruns"];
                    }).reverse();
                    // print results for testing
                    _.each(groupedObjects, function(obj)
                    {
                        var output = '';
                        _.each(obj, function(val, key)
                        {
                            output += key + ': ' + val + '&lt;br&gt;';
                        });
                        output += '&lt;br&gt;';
                        $('#results').append(output);
                        console.log(output);
                    });
                }
            });
        }
</code></pre>