2016-06-22 108 views
0

我有散列的數組:替換哈希值

[{"__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"}] 

我想用__content__值替換parentId值來獲得:

[{"__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

那麼'__content__'的值需要放在'parentId'中嗎? – Roger

回答

4

我寧願改變這種nasted arrayhash更好馬平。

輸入

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"} 
# ] 
+0

,它的工作原理。漂亮的複雜,但總體來說,它做了我所需要的。 thx – user

+0

我upvoted,但有兩個建議,以改善你的答案。首先,當你提出你將要使用的解決方案時,添加一個劣等的選擇(這裏使用'sort')會削弱你的答案。 (當有兩種方式做一些沒有明確勝利者的東西時,當然可以將它們都展示出來。)其次,我認爲大多數讀者希望在一個地方看到所有的代碼,而不是用解釋的方式來構建它。您可以先將代碼放入代碼中,然後解釋步驟(哪些讀者理解代碼可能會跳過),或者在展示代碼之前用文字說明步驟。 –

+0

@CarySwoveland感謝您的建議。我會盡力改進我的答案。 –

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 
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"}] 
+1

這是一個有趣的數組名稱。 –

+0

@CarySwoveland我喜歡糾結! – mudasobwa

+0

我注意到了。我,我被批評壓倒了。 –