How do I turn this:
[[3, 18], [5, 20], [7, 22]]
into this:
[
{:quantity=>3, :price=>18},
{:quantity=>5, :price=>20},
{:quantity=>7, :price=>22}
]
Hoping to get a one-liner answer, but I'll take what I can get.
Using Array#map:
a = [[3, 18], [5, 20], [7, 22]]
a.map { |item| {quantity: item[0], price: item[1]} }
# => [{:quantity=>3, :price=>18},
# {:quantity=>5, :price=>20},
# {:quantity=>7, :price=>22}]
a.map { |q, p| {quantity: q, price: p} }
# => [{:quantity=>3, :price=>18},
# {:quantity=>5, :price=>20},
# {:quantity=>7, :price=>22}]
a.map { |item| Hash[[:quantity, :price].zip(item)] }
# => [{:quantity=>3, :price=>18},
# {:quantity=>5, :price=>20},
# {:quantity=>7, :price=>22}]
Do as below using Array#map
a = [[3, 18], [5, 20], [7, 22]]
a.map { |v1,v2| {:quantity => v1, :price => v2} }