的阵列我有一个阵列像哈希:红宝石,我如何比较两对项目的散列
arry = {hash1, hash2, hash3,hash4 ....hash_N}
每个哈希,
hash1 ={ "deviceId"=>"868403021230682", "osVersion"=>"6.0", "deviceOS"=>"Android",
"appVersion"=>"1.7.2", "action"=>"Enter"}
hash2 = { "deviceId"=>"868403021230682", "osVersion"=>"6.0", "deviceOS"=>"Android",
"appVersion"=>"1.7.2", "action"=>"Leave"}
,因为它有可能为每个hash "action"=>"Enter" or "Leave"
不会总是显示为一对,例如,动作对于hash3,hash4,hash5可能都是“Enter”。我的想法是只考虑两个哈希谁可以做一个像hash1和hash2,从数组中删除其他数组或将其放入其他数组。 所以新阵列应该只包含[hash1, hash2, hash7,hash8]
,可以说hash7和8也是一对。
我应该使用each_with_index吗?我的代码是这样的:
def get_result(doc)
result = []
doc.each_slice(2).map { |pair|
pair.each_with_index { |element, index|
if (pair[index].has_value?([:action] => "enter") &&pair[index+1].has_value?([:action] => "Leave")
result.push(pair)
end
}
}
end
但if
语句不工作的权利,那种困惑如何使用each_with_index
希望有人能帮助我
我怎么能抛弃哈希值保存到另一个阵列?为什么需要平坦?我只想消除未配对的哈希。如果可能的话,你会介意一下吗?谢谢 – roccia
@roccia我更新了我的答案,希望对你有帮助 –
谢谢!这很清楚! – roccia