按列唯一哈希并合并其他列
Hash unique by column and merge other column
我有以下哈希。如何获得唯一和合并哈希。
[
{
"email"=>"user_name@aaa.com",
"expression"=>"aaa",
"name"=>"bbb",
"roles"=>[:admin]
},
{
"email"=>"user_name@aaa.com",
"expression"=>"aaa",
"name"=>"bbb",
"roles"=>[:manager]
}
]
我想要下面的结果散列,通过电子邮件唯一并按角色合并。
[
{
"email"=>"user_name@aaa.com",
"expression"=>"aaa",
"name"=>"bbb",
"roles"=>[:admin, :manager]
}
]
帮帮我。
试试这个
a.group_by { |item| item["email"] }.each_with_object([]) do |(_, items), arr|
arr.concat([items.first.merge("roles" => items.flat_map { |item| item["roles"] }.uniq)])
end
input.
group_by { |e| e['email'] }.
map do |_, v|
v.reduce do |acc, h|
acc.merge(h) { |_, v1, v2| v1 == v2 ? v1 : [*v1, *v2] }
end
end
#⇒ [{"email"=>"user_name@aaa.com",
# "expression"=>"aaa",
# "name"=>"bbb",
# "roles"=>[:admin, :manager]}]
arr = [{ "email"=>"user_name@aaa.com", "expression"=>"aaa", "name"=>"bbb",
"roles"=>[:admin] },
{ "email"=>"user_name@aaa.com", "expression"=>"aaa", "name"=>"bbb",
"roles"=>[:manager] }]
arr.first.merge("roles"=>arr.map { |h| h["roles"].first })
#=> {"email"=>"user_name@aaa.com", "expression"=>"aaa", "name"=>"bbb",
# "roles"=>[:admin, :manager]}
步骤如下
h = arr.first
#=> {"email"=>"user_name@aaa.com", "expression"=>"aaa", "name"=>"bbb",
# "roles"=>[:admin]}
a = arr.map { |h| h["roles"].first }
#=> [:admin, :manager]
g = { "roles"=>a }
#=> {"roles"=>[:admin, :manager]}
h.merge(g)
#=> {"email"=>"user_name@aaa.com", "expression"=>"aaa", "name"=>"bbb",
# "roles"=>[:admin, :manager]}
请注意,Ruby 允许将散列作为方法参数写入其键值对序列。例如:
receiver.doit({ a: 1, b: 2})
可以写
receiver.doit(a: 1, b: 2)
我有以下哈希。如何获得唯一和合并哈希。
[
{
"email"=>"user_name@aaa.com",
"expression"=>"aaa",
"name"=>"bbb",
"roles"=>[:admin]
},
{
"email"=>"user_name@aaa.com",
"expression"=>"aaa",
"name"=>"bbb",
"roles"=>[:manager]
}
]
我想要下面的结果散列,通过电子邮件唯一并按角色合并。
[
{
"email"=>"user_name@aaa.com",
"expression"=>"aaa",
"name"=>"bbb",
"roles"=>[:admin, :manager]
}
]
帮帮我。
试试这个
a.group_by { |item| item["email"] }.each_with_object([]) do |(_, items), arr|
arr.concat([items.first.merge("roles" => items.flat_map { |item| item["roles"] }.uniq)])
end
input.
group_by { |e| e['email'] }.
map do |_, v|
v.reduce do |acc, h|
acc.merge(h) { |_, v1, v2| v1 == v2 ? v1 : [*v1, *v2] }
end
end
#⇒ [{"email"=>"user_name@aaa.com",
# "expression"=>"aaa",
# "name"=>"bbb",
# "roles"=>[:admin, :manager]}]
arr = [{ "email"=>"user_name@aaa.com", "expression"=>"aaa", "name"=>"bbb",
"roles"=>[:admin] },
{ "email"=>"user_name@aaa.com", "expression"=>"aaa", "name"=>"bbb",
"roles"=>[:manager] }]
arr.first.merge("roles"=>arr.map { |h| h["roles"].first })
#=> {"email"=>"user_name@aaa.com", "expression"=>"aaa", "name"=>"bbb",
# "roles"=>[:admin, :manager]}
步骤如下
h = arr.first
#=> {"email"=>"user_name@aaa.com", "expression"=>"aaa", "name"=>"bbb",
# "roles"=>[:admin]}
a = arr.map { |h| h["roles"].first }
#=> [:admin, :manager]
g = { "roles"=>a }
#=> {"roles"=>[:admin, :manager]}
h.merge(g)
#=> {"email"=>"user_name@aaa.com", "expression"=>"aaa", "name"=>"bbb",
# "roles"=>[:admin, :manager]}
请注意,Ruby 允许将散列作为方法参数写入其键值对序列。例如:
receiver.doit({ a: 1, b: 2})
可以写
receiver.doit(a: 1, b: 2)