2016-04-11 19 views

答えて

1
check.flat_map{|c| input_hash.select{|aa| aa["id"] == c}}.map{|a| a["name"]}.join(", ") 
=> "george, nancy" 

または

input_hash.select{|h| h["name"] if check.include? h["id"]}.map{|aa| aa["name"]}.join(", ") 
=> "george, nancy" 
0

これを試してみてください:

def get_output_hash(input_hash, ids) 
    input_hash.each do |hash| 
    if ids.include?(hash["id"]) 
     p hash["name"] 
    end 
    end 
end 

コールはそれが好き: -

input_hash = [{"id"=>"123", "name"=>"ashly"}, {"id"=>"73", "name"=>"george"}, {"id"=>"175", "name"=>"nancy"}, {"id"=>"433", "name"=>"grace"}] 

get_output_hash(input_hash, ["73", "175"]) 
1
input_hash.select {|h| check.include?(h["id"])}.map {|h| h["name"]}.join(", ") 
+0

これは最後に結合( "、")を使用していました。ありがとう – user3636388

1
input_hash.map { |hash| hash["name"] if check.include?(hash["id"]) }.compact 
2
input_hash.map(&:values).to_h.values_at(*check).join(", ") 
# => "george, nancy" 
関連する問題