我有这些数组。我想只选择:teacher
数组中@teachers
值的实例,并将它们放在@filtered_courses
中。
@teachers = ["KAP", "CWD"]
@courses = [
{:gruppnamn=>"ESM15/15MUSINS01Str", :kurskod=>"MUSINS01S", :teacher=>"MWN", :class=>"ESM15", :points=>100.0, :kvot=>nil, :tim=>nil},
{:gruppnamn=>"ESM15/15MUSMUS01", :kurskod=>"MUSMUS01", :teacher=>"KAP", :class=>"ESM15", :points=>100.0, :kvot=>nil, :tim=>nil},
{:gruppnamn=>"Hälsostudion/15år2", :kurskod=>"IDRIDR01", :teacher=>"CWD", :class=>"ESSA", :points=>100.0, :kvot=>nil, :tim=>nil}
]
答案 0 :(得分:3)
尝试:
@filtered_courses = @courses.select { |x| @teachers.include? x[:teacher] }
# => [{:gruppnamn=>"ESM15/15MUSMUS01", :kurskod=>"MUSMUS01", :teacher=>"KAP", :class=>"ESM15", :points=>100.0, :kvot=>nil, :tim=>nil},
# {:gruppnamn=>"Hlsostudion/15r2", :kurskod=>"IDRIDR01", :teacher=>"CWD", :class=>"ESSA", :points=>100.0, :kvot=>nil, :tim=>nil}]