Reputation: 1060
I have these two arrays
Hotel = 'hilton', 'marriot'
Price = '$350', '$375'
How would I go about merging the two arrays together and making the price a key to the hotel.
So when I access
Price[0]
It outputs
'$350' => 'hilton' (or however the correct output should be)
Upvotes: 1
Views: 136
Reputation: 118261
Do as below using Array#zip
and Hash::[]
:
Hotel = 'hilton', 'marriot'
Price = '$350', '$375'
Hash[Price.zip(Hotel)]
# => {"$350"=>"hilton", "$375"=>"marriot"}
But to meet your posted description :
Hotel = 'hilton', 'marriot'
Price = '$350', '$375'
array_of_hash = Price.each_index.map { |i| { Price[i] => Hotel[i]} }
# => [{"$350"=>"hilton"}, {"$375"=>"marriot"}]
array_of_hash[0] # => {"$350"=>"hilton"}
Now choose, whatever way suits to your need.
Upvotes: 3