I have an array of hashes that looks like this:
array= [
{
"id"=> 101,
"first_name"=> "xxx",
"last_name"=> "G",
"email"=> "[email protected]",
"phone_number"=> "555-555-5555"
},
{
"id"=> 102,
"first_name"=> "Jen",
"last_name"=> "P",
"email"=> "[email protected]",
"phone_number"=> "555-555-5555"
}
]
I want to convert it to a hash that looks like this:
array = {
"101"=>
{
"first_name"=> "xxx",
"last_name"=> "G",
"email"=> "[email protected]",
"phone_number"=> "555-555-5555"
},
"102"=>
{
"first_name"=> "Jen",
"last_name"=> "P",
"email"=> "[email protected]",
"phone_number"=> "555-555-5555"
}
}
I have tried this but it does not work:
array.each do |a|
a.map{|x| x[:id]}
end
How can I do this in Ruby? I am looking at the map function, but not sure how to implement it in this case. Please help!
jsongroup_byas inarray.group_by{|a| a[:id]}.