Group and sum array of hashes by date
I have an array of hashes like this:
[{: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(:+)}}
Group and sum array of hashes by date的更多相关文章
- MySQL中group by , sum , case when then 的使用
在我们使用数据库的时候,可能会遇到需要进行统计的情况. 比如需要统计一下,下表中各个年份的胜负场数. 遇到这样的情况,我们应该怎么办呢? 在mysql中我们可以使用group by sum case ...
- [Swift通天遁地]五、高级扩展-(6)对基本类型:Int、String、Array、Dictionary、Date的扩展
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- [转]LINQ: Using INNER JOIN, Group and SUM
本文转自:https://stackoverflow.com/questions/530925/linq-using-inner-join-group-and-sum SELECT T1.Column ...
- 17-[JavaScript]- 内置对象 Array,String,Date,Math
1.Array数组 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> < ...
- 1.Two Sum (Array; HashTable)
Given an array of integers, find two numbers such that they add up to a specific target number. The ...
- 1. Two Sum [Array] [Easy]
Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...
- 类似于GROUP BY SUM() 用于字符串连接的语句
CREATE TABLE T ( [f1] VarCHAR(100), [f2] VarCHAR(100))goINSERT INTO T VALUES ('a','abc')INSERT INT ...
- LeetCode 548. Split Array with Equal Sum (分割数组使得子数组的和都相同)$
Given an array with n integers, you need to find if there are triplets (i, j, k) which satisfies fol ...
- [Swift]LeetCode813. 最大平均值和的分组 | Largest Sum of Averages
We partition a row of numbers A into at most K adjacent (non-empty) groups, then our score is the su ...
随机推荐
- 理解JavaScript的闭包
在JS这块,免不了被问什么是闭包. 从一个常见的循环问题说起. 有一个ul列表, 里面有5个li标签,我希望点击每个li标签的时候,弹出每个li标签对应的索引值(第一个弹出0,第二个弹出1...). ...
- sql pivot、unpivot和partition by用法
原文:sql pivot.unpivot和partition by用法 演示脚本 from sys.sysobjects where name = 'Student' AND type = 'U') ...
- 【SQL】Oracle的PL/SQL语法及其拓展数据类型总结
PL/SQL语法 PL/SQL程序由三部分组成,声明部分.执行部分.异常处理部分. 模板: DECLARE /*变量声明*/ BEGIN /*程序主体*/ EXCEPTION /*异常处理部分*/ E ...
- 了解HTML5和“她”的 API (一)
简化了文档声明.字符集 //声明 <!doctype html> //字符 <meta charset="utf-8"> 引入了新的标签元素 Select ...
- 为了解决这个问题:07文本WORD文档超链接、页码成{HYPERLINK"网站"}、{PAGE}/{NUMPAGES}
版权声明:本文博主原创文章.博客,未经同意不得转载.
- system strategies of Resources Deadlock
In computer science, Deadlock is a naughty boy aroused by compete for resources. Even now, there ...
- Direct3D 使用质地
关于使用质地 1 创建纹理 2 纹理寻址模式 3 纹理过滤 1 创建纹理 <1> D3DXCreateTexture功能 创建一个空的纹理. HRESULT D3DXCreateText ...
- Oracle中merge into的使用 (转)
http://blog.csdn.net/yuzhic/article/details/1896878 http://blog.csdn.net/macle2010/article/details/5 ...
- 职业选择測试(A/B卷)
不同性格的人适合从事不同的职业.职业选择对于每一个人都是很重要的事情.假设能选一个既可以发挥潜能又有兴趣的工作,会使整个团队的效率逐倍增长.想了解你更适合什么职业吗?一起来測试一下吧.本套測试分为A卷 ...
- 简单实现Android平台多语言
这里,我们认识到两种语言.中国简体和繁体中国. 在res文件建议两个文件夹 values-zh-rCN values-zh-rTW 两个目录下都有一个strings.xml文件. 两个同名文件的字符串 ...