Please take a look at the below code
def test
array = Array.new
array2 = Array.new
groups = [[424235, "goa", "italy"], [523436, "mumbai"], [342423, "africa", "goa"]]
type = ["goa", "mumbai"]
groups.each_with_index do |item,index|
if item.include?(type[0]) == true
array << index << array2
elsif item.include?(type[1]) == true
array2 << index
else
"nothing ;)"
end
end
print array.each_slice(2).map { |a, b| [a, b.first] }
end
combine
#Output - [[0, 1], [2, 1]]
See the problem with the code? That is I am using a bunch of if and else statements. What if type array has more than 2 entries. I cant go on writing the if and elsif statements. And thats where I need your help. What is a better what to structure the code? loops? if so how.