如何根据深度嵌套的散列数组中的键值合并散列数组

时间:2017-10-14 10:01:19

标签: arrays ruby hash

我有一个像这样的数组

@info = [
  {"id"=>22014, "act"=>[{"data"=>["show", "video", "audio"], "stud_id"=>11318}]},
  {"id"=>22014, "act"=>[{"data"=>["play", "pause", "start"], "stud_id"=>11439}]},
  {"id"=>22025, "data"=>[{"data"=>["music"], "stud_id"=>11451}]},
  {"id"=>22078, "data"=>[{"data"=>["radio"], "stud_id"=>11705}]},
  {"id"=>22050, "act"=>[{"data"=>["switchon", "switchoff", "flatmusic"], "stud_id"=>11099}]}
]

我想从上面的数组中得到这样的数组

@info = [
  {"id"=>22014, "act"=>[{"data"=>["show", "video", "audio"], "stud_id"=>11318}, {"data"=>["play", "pause", "start"], "stud_id"=>11439}]},
  {"id"=>22025, "data"=>[{"data"=>["music"], "stud_id"=>11451}]},
  {"id"=>22078, "data"=>[{"data"=>["radio"], "stud_id"=>11705}]},
  {"id"=>22050, "act"=>[{"data"=>["switchon", "switchoff", "flatmusic"], "stud_id"=>11099}]}
]

如何根据常用ID值合并?

2 个答案:

答案 0 :(得分:2)

info = [
  {"id"=>22014, "act"=>[{"data"=>["show", "video", "audio"], "stud_id"=>11318}]},
  {"id"=>22014, "act"=>[{"data"=>["play", "pause", "start"], "stud_id"=>11439}]},
  {"id"=>22025, "act"=>[{"data"=>["music"], "stud_id"=>11451}]},
  {"id"=>22078, "act"=>[{"data"=>["radio"], "stud_id"=>11705}]},
  {"id"=>22050, "act"=>[{"data"=>["switchon", "switchoff", "flatmusic"], "stud_id"=>11099}]}
]

def combine(info)
  info.group_by {|g|g["id"]}.values.map {|a| {"id" => a.first["id"], "act"=>a.map {|g| g["act"]}}} 
end 

combine info

output

 [{"id"=>22014, "act"=>[[{"data"=>["show", "video", "audio"], "stud_id"=>11318}], [{"data"=>["play", "pause", "start"], "stud_id"=>11439}]]},
 {"id"=>22025, "act"=>[[{"data"=>["music"], "stud_id"=>11451}]]},
 {"id"=>22078, "act"=>[[{"data"=>["radio"], "stud_id"=>11705}]]},
 {"id"=>22050, "act"=>[[{"data"=>["switchon", "switchoff", "flatmusic"], "stud_id"=>11099}]]}]

答案 1 :(得分:0)

这是非常基本的Ruby。我留给你not ids_act && h_act

的情况
info = [
  {"id"=>22014, "act"=>[{"data"=>["show", "video", "audio"], "stud_id"=>11318}]},
  {"id"=>22014, "act"=>[{"data"=>["play", "pause", "start"], "stud_id"=>11439}]},
  {"id"=>22025, "data"=>[{"data"=>["music"], "stud_id"=>11451}]},
  {"id"=>22078, "data"=>[{"data"=>["radio"], "stud_id"=>11705}]},
  {"id"=>22050, "act"=>[{"data"=>["switchon", "switchoff", "flatmusic"], "stud_id"=>11099}]}
]

ids = {}

info.each do | h |
    puts "h=#{h}"
    h_id     = h["id"]
    id_found = ids[h_id] # nil or previously encountered id
    puts "id_found=#{id_found.inspect}"

    if id_found
    then # concatenate act content
        h_act   = h["act"]
        ids_act = id_found["act"]
        ids_act = ids_act.concat(h_act) if ids_act && h_act
    else # store the current hash
        ids[h_id] = h
    end
end

new_info = ids.collect{ | k, v | v }
puts '--- new_info ---'
puts new_info

执行:

$ ruby -w t.rb 
h={"id"=>22014, "act"=>[{"data"=>["show", "video", "audio"], "stud_id"=>11318}]}
id_found=nil
h={"id"=>22014, "act"=>[{"data"=>["play", "pause", "start"], "stud_id"=>11439}]}
id_found={"id"=>22014, "act"=>[{"data"=>["show", "video", "audio"], "stud_id"=>11318}]}
h={"id"=>22025, "data"=>[{"data"=>["music"], "stud_id"=>11451}]}
id_found=nil
h={"id"=>22078, "data"=>[{"data"=>["radio"], "stud_id"=>11705}]}
id_found=nil
h={"id"=>22050, "act"=>[{"data"=>["switchon", "switchoff", "flatmusic"], "stud_id"=>11099}]}
id_found=nil
--- new_info ---
{"id"=>22014, "act"=>[{"data"=>["show", "video", "audio"], "stud_id"=>11318}, {"data"=>["play", "pause", "start"], "stud_id"=>11439}]}
{"id"=>22025, "data"=>[{"data"=>["music"], "stud_id"=>11451}]}
{"id"=>22078, "data"=>[{"data"=>["radio"], "stud_id"=>11705}]}
{"id"=>22050, "act"=>[{"data"=>["switchon", "switchoff", "flatmusic"], "stud_id"=>11099}]}