假设我有 2 个哈希数组,如下所示,
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) 合并/比较/过滤这两个哈希,以便仅打印在 updated_at(local_todos) 和更新 (google_tasks) 值中存在差异的 id/api_id(两者都是相同的值)作为输出。
期望的输出将是这样的,
["1234567", "098098"]
因为如果您检查这两个 ID 具有不同的更新/updated_at值。
有什么帮助吗?
只是简单的选择:
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
在我的工作台结果中,第一个代码稍微快一点。
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
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