jagga99
jagga99

Reputation: 47

Find items from the array of strings in array of hashes and proceed with the appropriate hashes

I have an array of links:

array = [link1, link2, link3, link4]
and array_of_hashes with two items: :names and :links

hash = { :names, :links } e.g.
array_of_hashes = [{ :names => name5, :links => link5}, {:names = name1, :links => link1}, ... ]

I want to do something with each pair of hashes (:names :links) from array_of_hashes which including links from the original array of links.

Thus, at final stage I need to find pair of hashes (in my case listed above):

{:names => name1, :links => link1}

cause link1 are listed in the array with links

UPD: Revised data... sorry for missunderstanding. Thanks a lot for your assistance.

Upvotes: 0

Views: 144

Answers (1)

Jon M
Jon M

Reputation: 11705

If I understand your question correctly, this should do what you want:

# 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]}

This gives the output => [{:names=>"name1", :links=>"link1"}]

Upvotes: 2

Related Questions