0

I have these arrays. And i want to select only the instances with the :teacher value from the @teachers array and put them in @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}
]
1
  • 1
    The method to select certain elements from an array is called select Commented Mar 21, 2016 at 11:32

1 Answer 1

3

Try:

@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}]
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! Solved it!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.