我有一系列哈希:
[{"__content__"=>"Guitars", "id"=>"2"},
{"__content__"=>"For Guitars", "id"=>"3", "parentId"=>"2"},
{"__content__"=>"6-string", "id"=>"24", "parentId"=>"2"},
{"__content__"=>"7-string", "id"=>"25", "parentId"=>"2"},
{"__content__"=>"8-string", "id"=>"26", "parentId"=>"2"}]
我想将parentId
值替换为__content__
值,以获取:
[{"__content__"=>"Guitars", "id"=>"2"},
{"__content__"=>"For Guitars", "id"=>"3", "parentId"=>"Guitars"},
{"__content__"=>"6-string", "id"=>"24", "parentId"=>"Guitars"},
{"__content__"=>"7-string", "id"=>"25", "parentId"=>"Guitars"},
{"__content__"=>"8-string", "id"=>"26", "parentId"=>"Guitars"}]
我该怎么做?
答案 0 :(得分:4)
我更喜欢将这个禁止的array
更改为hash
以便更好地制作maping。
<强>输入强>
arr = [
{"__content__" => "Guitars", "id" => "2"},
{"__content__" => "For Guitars", "id" => "3", "parentId" => "2"},
{"__content__" => "6-string", "id" => "24", "parentId" => "2"},
{"__content__" => "7-string", "id" => "25", "parentId" => "2"},
{"__content__" => "8-string", "id" => "26", "parentId" => "2"}
]
创建父母
parents = arr.each_with_object({}) { |h, exp| exp[h['id']] = h['__content__'] }
在这个良好的地图结构之后,您只需使用map
方法。
<强>过程强>
output = arr.map do |h|
h['parentId'] = parents[h['parentId']] if h['parentId'] && parents[h['parentId']]
h
end
<强>输出强>
p output
# [
# {"__content__" => "Guitars", "id" => "2"},
# {"__content__" => "For Guitars", "id" => "3", "parentId" => "Guitars"},
# {"__content__" => "6-string", "id" => "24", "parentId" => "Guitars"},
# {"__content__" => "7-string", "id" => "25", "parentId" => "Guitars"},
# {"__content__" => "8-string", "id" => "26", "parentId" => "Guitars"}
# ]
我希望这会有所帮助。
修改强>
你也可以这样使用
按ID排序
arr.sort_by! { |h| h['id'] }
<强>过程强>
output = arr.each_with_object({}) do |h, exp|
exp[h['id']] = h
h['parentId'] = exp[h['parentId']]['__content__'] if h['parentId'] && exp[h['parentId']]
h
end
在此之后你将拥有像键一样的id哈希,所以对你的输出:
<强>输出强>
p output.values
# [
# {"__content__"=>"Guitars", "id"=>"2"},
# {"__content__"=>"6-string", "id"=>"24", "parentId"=>"Guitars"},
# {"__content__"=>"7-string", "id"=>"25", "parentId"=>"Guitars"},
# {"__content__"=>"8-string", "id"=>"26", "parentId"=>"Guitars"},
# {"__content__"=>"For Guitars", "id"=>"3", "parentId"=>"Guitars"}
# ]
答案 1 :(得分:1)
小心!下面的解决方案改变了原始哈希!
▶ hashes = [{"__content__"=>"Guitars", "id"=>"2"},
▷ {"__content__"=>"For Guitars", "id"=>"3", "parentId"=>"2"},
▷ {"__content__"=>"6-string", "id"=>"24", "parentId"=>"2"},
▷ {"__content__"=>"7-string", "id"=>"25", "parentId"=>"2"},
▷ {"__content__"=>"8-string", "id"=>"26", "parentId"=>"2"}]
hashes.each_with_object({}) do |h, memo|
memo[h['id']] = h['__content__']
h['parentId'] = memo[h['parentId']]
end
hashes
#⇒ [{"__content__"=>"Guitars", "id"=>"2", "parentId"=>nil},
# {"__content__"=>"For Guitars", "id"=>"3", "parentId"=>"Guitars"},
# {"__content__"=>"6-string", "id"=>"24", "parentId"=>"Guitars"},
# {"__content__"=>"7-string", "id"=>"25", "parentId"=>"Guitars"},
# {"__content__"=>"8-string", "id"=>"26", "parentId"=>"Guitars"}]
答案 2 :(得分:0)
arr = [{"__content__"=>"Guitars", "id"=>"2"}, {"__content__"=>"For Guitars", "id"=>"3", "parentId"=>"2"}]
id_content_mapping = arr.reduce({}) do |h, elt|
h[elt["id"]] = elt["__content__"]
h
end
arr.map do |elt|
if elt.has_key? "parentId"
elt["parentId"] = id_content_mapping[elt["parentId"]]
end
elt
end