I have an array of hashes coming from a dynamo table that I need to group by a key and sum the values of another key. My array looks similar to:
data = [
  { 'state' => 'Florida', 'minutes_of_sun' => 10, 'timestamp' => 1497531600, 'region' => 'Southeast' },
  { 'state' => 'Florida', 'minutes_of_sun' => 7, 'timestamp' => 1497531600, 'region' => 'Southeast' },
  { 'state' => 'Florida', 'minutes_of_sun' => 2, 'timestamp' => 1497531600, 'region' => 'Southeast' },
  { 'state' => 'Georgia', 'minutes_of_sun' => 15, 'timestamp' => 1497531600, 'region' => 'Southeast' },
  { 'state' => 'Georgia', 'minutes_of_sun' => 5, 'timestamp' => 1497531600, 'region' => 'Southeast' }
]
The end result that I would be looking for is:
data = [
  { 'state' => 'Florida', 'minutes_of_sun' => 19, 'region' => 'Southeast' },
  { 'state' => 'Georgia', 'minutes_of_sun' => 20, 'region' => 'Southeast' }
]
I've been able to do this via a method I wrote below, but it's slow and clunky. Was wondering if there is a faster/less LoC way to do this?
def combine_data(data)
  combined_data = []
  data.each do |row|
    existing_data = combined_data.find { |key| key['state'] == row['state'] }
    if existing_data.present?
      existing_data['minutes_of_sun'] += row['minutes_of_sun']
    else
      combined_data << row
    end
  end
  combined_data
end