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的更多相关文章

  1. MySQL中group by , sum , case when then 的使用

    在我们使用数据库的时候,可能会遇到需要进行统计的情况. 比如需要统计一下,下表中各个年份的胜负场数. 遇到这样的情况,我们应该怎么办呢? 在mysql中我们可以使用group by sum  case ...

  2. [Swift通天遁地]五、高级扩展-(6)对基本类型:Int、String、Array、Dictionary、Date的扩展

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  3. [转]LINQ: Using INNER JOIN, Group and SUM

    本文转自:https://stackoverflow.com/questions/530925/linq-using-inner-join-group-and-sum SELECT T1.Column ...

  4. 17-[JavaScript]- 内置对象 Array,String,Date,Math

    1.Array数组 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> < ...

  5. 1.Two Sum (Array; HashTable)

    Given an array of integers, find two numbers such that they add up to a specific target number. The ...

  6. 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 ...

  7. 类似于GROUP BY SUM() 用于字符串连接的语句

    CREATE TABLE T ( [f1] VarCHAR(100), [f2] VarCHAR(100))goINSERT INTO T   VALUES ('a','abc')INSERT INT ...

  8. 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 ...

  9. [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 ...

随机推荐

  1. 流动python - 什么是魔术方法(magic method)

    我们经常看到各种各样的方法已经被包围了由双下划线,例如__init__,他们是魔术方法. 魔术方法python语言预订好"协议",在不同情况下不同的魔术方法,是隐式调用.我们重写这 ...

  2. Web指纹识别目的Discuz识别+粗糙的版本演绎

    这个识别程序是本学期在我的职业培训项目.它是做一类似至Zoomeye怪东西,然后使用ES集成,为了让搜索引擎寻找.因此,我们必须首先去网上识别相应的能力Web包裹,如果用户输入的关键词:Discuz ...

  3. CentOS6.5设备MRBS

    //--------------------------------------软件必须安装-----------------------------------// # yum install –y ...

  4. poj2524

    说来惭愧啊..现在才会并查集.我竟然给我妈妈讲明白并查集怎么回事了- - #define _CRT_SECURE_NO_WARNINGS #include <iostream> using ...

  5. unix pwd使用命令

    [语法]:     pwd [说明]:    此命令会显示当前的工作文件夹 []: pwd     这显示当前工作文件夹 版权声明:本文博主原创文章.博客,未经同意不得转载.

  6. 实现一个简单的boot

    1.汇编语言.分别汇编器和链接as86和ld86.码如下面: .globl begtext,begdata,begbss,endtext,enddata,endbss .text begtext: . ...

  7. c#-RTF文本编辑器

    1".RTF"什么? 多信息文本格式 (RTF) 是一种方便于不同的设备.系统查看的文本和图形文档格式. RTF 使用美国国内标准协会 (ANSI). PC-8. Macintos ...

  8. web 环境项目(intellj部署的tomcat) 重启时报 Exception in thread "HouseKeeper" java.lang.NullPointerException (转)

    Exception in thread "HouseKeeper" java.lang.NullPointerException at org.logicalcobwebs.pro ...

  9. Docker简明教程(转)

    Docker自从诞生以来就一直备受追捧,学习Docker是一件很炫酷.很有意思的事情.我希望通过这篇文章能够让大家快速地入门Docker,并有一些学习成果来激发自己的学习兴趣.我也只是一个在Docke ...

  10. EnumMap demo

    优点:常量做为Key,在编译期就确定了.Enum做为key,在运行时也可以改变 package enumdemo; import java.util.EnumMap; import java.util ...