a = [
{ :color => "blue", :name => "wind" },
{ :color => "red", :name => "fire" },
{ :color => "white", :name => "wind" },
{ :color => "yellow", :name => "wind" },
{ :color => "green", :name => nil },
{ :color => "white", :name => "snow" }
]
I have another hash
b = { blue: 'blue', white: 'white', red: 'red', green: 'green' }
The result I need is:
['wind', 'fire']
# To find out uniq names based on the input hash.
What I tried:
names = a.map{ |i| {i[:color], i[:name]} }.delete_if { |key, value| value.nil? }
resultant_names = []
b_values = b.values
b_values.each do |c|
if names[c]
resultant_names << names[c]
end
end
resultant_names.uniq
Need a better approach please. This goes with too many loops.
Aucun commentaire:
Enregistrer un commentaire