我有一系列链接:
array = [link1, link2, link3, link4]和array_of_hashes有两个项目:: names和:links
hash = { :names, :links } e.g. array_of_hashes = [{ :names => name5, :links => link5}, {:names = name1, :links => link1}, ... ]
我想对array_of_hashes中的每对哈希(:names:links)做一些事情,其中包括原始链接数组的链接。
因此,在最后阶段我需要找到一对哈希(在我的案例中列出):
{:names => name1, :links => link1}
因为link1在数组中列出了链接
UPD:修改后的数据......对不起有误。非常感谢您的帮助。
答案 0 :(得分:2)
如果我理解你的问题,这应该做你想要的:
# Cleaned up the setup a little
array_of_hashes = [
{:names => 'name5', :links => 'link5'},
{:names => 'name1', :links => 'link1'},
{:names => 'name9', :links => 'link9'}]
array = ['link5', 'link1']
# This will filter the array
array_of_hashes.select{|x| array.include? x[:links]}
这给出了输出=> [{:names=>"name1", :links=>"link1"}]