Ruby, unique hashes in array based on multiple fields
来源:互联网 发布:业余足球平台软件 编辑:程序博客网 时间:2024/06/14 01:38
I'd like to get back an array of hashes based on sport and type combination
I've got the following array:
[ { sport: "football", type: 11, other_key: 5 }, { sport: "football", type: 12, othey_key: 100 }, { sport: "football", type: 11, othey_key: 700 }, { sport: "basketball", type: 11, othey_key: 200 }, { sport: "basketball", type: 11, othey_key: 500 }]
I'd like to get back:
[ { sport: "football", type: 11, other_key: 5 }, { sport: "football", type: 12, othey_key: 100 }, { sport: "basketball", type: 11, othey_key: 200 },]
I tried to use (pseudocode):
[{}, {}, {}].uniq { |m| m.sport and m.type }
I know I can create such array with loops, I'm quite new to ruby and I'm curious if there's a better (more elegant) way to do it.
Try using Array#values_at to generate an array to uniq
by.
sports.uniq{ |s| s.values_at(:sport, :type) }
0 0
- Ruby, unique hashes in array based on multiple fields
- ruby merge array of hashes based on the key and order it by number of key/value pair
- Condensing an array of hashes within that same array in Ruby
- Extracting Data from array of hashes Ruby
- Ruby: Count unique elements and their occurences in an array
- Ruby converting array of hashes to array of arrays
- ruby - group by repeating key of multiple hashes
- How to sort an array of hashes into hashes with multiple values for a key?
- array summation and merging based on uniqueness of element at certain position + ruby arrays
- how to merge array of hash based same keys in ruby?
- How to combine the value of multiple hashes within an array by the same key
- ruby find entry in array
- Whats the best way to split an array in ruby into multiple smaller arrays of random size
- array x array matrix in ruby
- MVC in Ruby on Rails
- RESTful in ruby on rails
- Ruby的散列表(Hashes)
- How do I sort an array of hashes by a value in the hash?
- Eclipse--Hadoop2.4.0开发环境
- android TextView不用ScrollViewe也可以滚动的方法
- Cocos2d-x 中Android.mk自动生成.cpp与.c类路径
- 二进制运算之sg笔试题
- Protocol Buffers
- Ruby, unique hashes in array based on multiple fields
- 一段完整的用jquery遍历json并显示到table中的客户端代码
- 开源项目
- 条件随机场(CRF)
- Java关于空字符串的正确判断
- Linux slab 分配器剖析
- xxs攻击
- 关于Oracle数据库删除、查询(二)
- Hadoop 高级程序设计(三)---自定义Partition和Combiner