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 ...
随机推荐
- html学习笔记二
html图片标记 <html> <head> <title>图片演示</title> </head> ----------------图片演 ...
- PHP第七课 使用方法数组2
学习平台: 1.了解数组函数 2.输出随机验证码 1.数组函数: 数组函数: //作用:提供了非常多官方写的非常实用的代码段,提高编写速度. 1.数组的键值操作函数 2.统计数组的元素和唯一性 3.使 ...
- Win10打不开chm文件的解决办法
在Win10系统中打开chm文件时,提示安全警告,如下图: 点击打开后,可以看到chm文件的目录大纲,但是点击任意目录其内容都是空白,如下图: 经过网络一通搜索,发现解决办法很简单,就是在上面的安装警 ...
- c# 用正则表达式在指定的字符串中每隔指定个数的文字插入指定字符串
public static string AddNewLine(string inString,int num,string addString="\r\n") { return ...
- 【转】window.scroll 浏览器滚动条的参数总结
如内容超出单元格,则隐藏style="TABLE-LAYOUT: fixed" 让弹出窗口总是在最上面: <body onblur="this.focus();&q ...
- 代理下载android4.4源代码
前提条件:需要有user, password, 代理人serverip和port(这一切都使自己的软件来完成下一个.例如freegate,它拥有一套可以自己作为一个代理server.创user/pas ...
- js实现鼠标拖拽div-------Day44
假设去问这样一个问题"你认为鼠标操作简单,还是键盘操作简单",相信会有多数人都会回答鼠标吧,毕竟键盘button那么多,假设手小了或者手法不规范了,太easy出问题了,也对操作的速 ...
- OpenGL【2 坐标转换】
// OpenGL.cpp : 自定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> #include & ...
- 如何识别SQL Server中的IO瓶颈
原文:如何识别SQL Server中的IO瓶颈 原文出自: http://www.mssqltips.com/sqlservertip/2329/how-to-identify-io-bottlene ...
- Tri_integral Summer Training 5 总结
比赛 题目 B D E G H I J 这是泰国的一场区域赛,除了C题英语非常抽以外,其余题目还不算难读. 一开场就发现了三道很水的题目,0:21:34就把三道题给过了,都是1A,赞Moor的手速. ...