Ruby - 基于1键比较/合并2个哈希数组

时间:2014-01-06 12:23:03

标签: ruby key compare hashtable hash

假设我有两个哈希数组,如下所示,

local_todos = [{name: "abc", title: "abcdwer", api_id: "1234567", updated_at: "2013-22-12"},
{name: "abcd", title: "abcdwe", api_id: "098098", updated_at: "2013-22-11"},
{name: "abcde", title: "abcdqw", api_id: "345345", updated_at: "2013-22-18"},
{name: "abcdef", title: "abcder", api_id: "234456", updated_at: "2013-22-15"}]

google_tasks = [{name: "abc", title: "xxxxx", id: "1234567", updated: "2013-22-19"},
{name: "abcd", title: "zzzzz", id: "098098", updated: "2013-22-15"},
{name: "abcde", title: "abcdqw", id: "345345", updated: "2013-22-18"},
{name: "abcdef", title: "abcder", id: "234456", updated: "2013-22-15"}]

现在我想,合并/比较/过滤这两个哈希纯粹基于api_id(local_todos)和id(google_tasks),以便只有id / api_id(两者都是相同的值)才有区别在updated_at(local_todos)和更新(google_tasks)值中打印为输出

所需的输出将是这样的,

["1234567", "098098"]

因为如果你检查这两个ID有不同的updated / updated_at值。

任何帮助?

3 个答案:

答案 0 :(得分:2)

local_todos.inject([]) do |result,l_td|
    if found = google_tasks.detect {|g_td| g_td[:id] == l_td[:api_id] and  g_td[:updated] != l_td[:updated_at]}
        result << found[:id]
    end
    result
end

答案 1 :(得分:2)

只需简单选择:

local_todos.map do | v1 |
   google_tasks.any? {|v2| v1[ :api_id ] == v2[ :id ] && v1[ :updated_at ] != v2[ :updated ] } && v1[ :api_id ] || nil
end.compact
# => ["1234567", "098098"]

要比较代码,您可以使用以下代码:

funcs =
[ proc { local_todos.map {|v1| google_tasks.any? {|v2| v1[ :api_id ] == v2[ :id ] && v1[ :updated_at ] != v2[ :updated ] } && v1[ :api_id ] || nil }.compact },
  proc {
   local_todos.inject([]) do |result,l_td|
    if found = google_tasks.detect {|g_td| g_td[:id] == l_td[:api_id] and  g_td[:updated] != l_td[:updated_at]}
        result << found[:id]
    end
    result
  end
},
]

def ctime func
   time = 0
   1000.times { time += Benchmark.measure { 1000.times { func.call } }.to_a[5].to_f }
   rtime = time /= 1000000
end

funcs.each {| func | p ctime( func ) }

# 3.9753190517425536e-05
# 4.056975722312927e-05

在我的替补席结果中,第一个代码稍快一些。

答案 2 :(得分:0)

local_todos.collect do |l_todo|
  google_tasks.collect do |g_task|
    l_todo[:api_id] if (l_todo[:api_id] == g_task[:id] && l_todo[:updated_at] != g_task[:updated])
  end.compact
end.flatten