Group and sum array of hashes by date
2015-05-09 19:39
441 查看
I have an array of hashes like this:
I'd like to group those hashes by date and sum the amounts. So, in my example, the output would be:
[/code]
[{:created=>Fri, 22 Jan 2014 13:02:13 UTC +00:00, :amount=>20}, {:created=>Fri, 27 Jan 2014 13:14:57 UTC +00:00, :amount=>15}, {:created=>Fri, 27 Jan 2014 14:42:40 UTC +00:00, :amount=>10}, {:created=>Fri, 28 Jan 2014 15:26:58 UTC +00:00, :amount=>10}, {:created=>Fri, 28 Jan 2014 15:30:18 UTC +00:00, :amount=>20}, {:created=>Fri, 31 Jan 2014 15:32:46 UTC +00:00, :amount=>50}, {:created=>Fri, 31 Jan 2014 15:33:29 UTC +00:00, :amount=>40}]
I'd like to group those hashes by date and sum the amounts. So, in my example, the output would be:
[{:created=>"2014-01-22", :amount=>20}, {:created=>"2014-01-27", :amount=>25}, {:created=>"2014-01-28", :amount=>30}, {:created=>"2014-01-31", :amount=>90}]
require 'date' a = [{:created=> 'Fri, 22 Jan 2014 13:02:13 UTC +00:00', :amount=>20}, {:created=>'Fri, 27 Jan 2014 13:14:57 UTC +00:00', :amount=>15}, {:created=>'Fri, 27 Jan 2014 14:42:40 UTC +00:00', :amount=>10}, {:created=>'Fri, 28 Jan 2014 15:26:58 UTC +00:00', :amount=>10}, {:created=>'Fri, 28 Jan 2014 15:30:18 UTC +00:00', :amount=>20}, {:created=>'Fri, 31 Jan 2014 15:32:46 UTC +00:00', :amount=>50}, {:created=>'Fri, 31 Jan 2014 15:33:29 UTC +00:00', :amount=>40}]a.group_by { |h| Date.parse h[:created] }.map { |k,v|{:created => k.to_s,:amount => v.map {|h1| h1[:amount]}.inject(:+)}}
[/code]
相关文章推荐
- Group and sum array of hashes by date
- How to SUM and GROUP BY of JSON data?
- ruby merge array of hashes based on the key and order it by number of key/value pair
- JavaScript SUM and GROUP BY of JSON data
- mysql5.7 报错1055:Expression #1 of SELECT list is not in GROUP BY clause and contains non
- Error Code: 1055. Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated
- [Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated c
- ruby - group by repeating key of multiple hashes
- #1055 - Expression of SELECT list is not in GROUP BY clause and contains nonaggregated column this i
- mysql遇见Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggre的问题
- SQL---Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column '
- [mysql] Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'lo
- Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'userinfo.
- MySQL报错“Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggre”
- Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column 'inform
- Sorting a mixed array of hashes and strings
- MySQL报错“Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggre”
- mysql错误:Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'ga
- [Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated c
- Test of MV FAST refresh with Sum and group