Voss Voss - 2 months ago 16
Ruby Question

How do I find an array of strings in an array of arrays?

I have an array of numbers I need to match in an array of arrays. How do I match the corresponding array?

Here is the first array:

animals_to_owners = [
["animals to owners"],
["animal id", " owner id"],
["67", " 31"],
["67", " 99"],
["67", " 53"],
["67", " 13"],
["346", " 99"],
["346", " 53"]
]


And my other array of owner ids:

owner_ids = ["99", "31", "53"]


I've tried
.select
,
.each_index
,
.include?
with no luck, I think it's because I'm comparing arrays instead of a single string to an array.

Ultimately I want to find the
animal_id
.

Thanks in advance.

ndn ndn
Answer
animals_to_owners.select do |_, owner_id|
  owner_ids.include? owner_id.to_s.strip
end.map(&:first)

Note that the weird to_s.strip part is needed due to the spaces in the owner ids.