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. RH033读书笔记(11)-Lab 12 Configuring the bash Shell

    Sequence 1: Configuring the bash Shell Deliverable: A system with new aliases that clear the screen, ...

  2. Apple Swift编程语言入门

    1   简单介绍 今天凌晨Apple刚刚公布了Swift编程语言,本文从其公布的书籍<The Swift Programming Language>中摘录和提取而成.希望对各位的iOS&a ...

  3. 开源Math.NET基础数学类库使用(03)C#解析Matlab的mat格式

    原文:[原创]开源Math.NET基础数学类库使用(03)C#解析Matlab的mat格式 开源Math.NET基础数学类库使用系列文章总目录:   1.开源.NET基础数学计算组件Math.NET( ...

  4. CocoStudio学习资源

    CocoStudio安装包及实例project:http://www.cocoachina.com/bbs/read.php?tid=154886 CocoStudio视频教程系列:http://mo ...

  5. Kotlin

    关于Kotlin,网上已有一些介绍的文章,包括Antonio Leiva的这组blog翻译稿.不过,我还是想跟进它们.翻译它们,以锻炼自己的英文翻译.各位高手发现问题,请及时“拍砖”. 原文题目:Ko ...

  6. 买面包和IoC

    今天上午准备去一个阿姨,在那里买面包.这可能是由于小尺寸她的,因此,管理不规范,所以,当你买面包.没有人行.即使所有的大学生,似几乎没有这种意识.. . 但让我感到震惊的是.尽管没有排队,但阿姨似乎能 ...

  7. POJ3581:Sequence(后缀数组)

    Description Given a sequence, {A1, A2, ..., An} which is guaranteed A1 > A2, ..., An,  you are to ...

  8. Binary Tree Inorder Traversal(转)

    Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary tr ...

  9. C#-默认显示前列-ShinePans

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  10. 【C疯狂的教材】(四)C语言分支语句

    1.程序的结构 程序默认从上到下顺序运行(顺序结构) 程序的结构:顺序结构.分支结构.循环结构 2.if分支语句 程序运行的过程中能够有多个选择 格式: if(表达式){ 语句块; } ...... ...