我想修改我的哈希数组以提高搜索性能
我有:
a = [ {"id" => 1, "name" => "Matt", "email" => "matt@example.com"}, {"id" => 2, "name" => "Charlie", "email" => "charlie@example.com"} ]
我想将其转换为:
b = [ {1 => { "name" => "Matt", "email" => "matt@example.com"}},{2 => { "name" => "Charlie", "email" => "charlie@example.com"}} ]
请注意,“id”字段不一定是连续的或连续的集合,但每次出现都是唯一的。此外,作为值嵌套在 b 中的散列可以包含“id”键/值对,如果这使事情变得更容易的话。