【发布时间】:2013-06-03 17:03:44
【问题描述】:
我查询了我的数据库,它给了我一个哈希数组,其中哈希中的键是列名。我只想保留根据多个(3列)唯一的哈希(数组元素)。我试过了:
array.uniq { |item| item[:col1], item[:col2], item[:col3] }
还有
array = array.inject([{}]) do |res, item|
if !res.any? { |h| h[:col1] == item[:col1] &&
h[:col2] == item[:col2] &&
h[:col3] == item[:col3] }
res << item
end
end
有没有人知道问题出在哪里或有其他解决方法?
谢谢
【问题讨论】:
-
提供样例输入输出就好了。
标签: ruby arrays attributes unique