2015-09-07 44 views
0

我想擺脫散列中每個屬性中的value: <value>關鍵值。並使其如下所示:"total_interactions": 493.667 下面是不正確的格式,我希望在json中實現預期的良好格式。散列中的哈希修改問題,替換紅寶石中的值

{ 
    "3": { 
     "total_interactions": { 
      "value": 493.667 
     }, 
     "shares": { 
      "value": 334 
     }, 
     "comments": { 
      "value": 0 
     }, 
     "likes": { 
      "value": 159.66666666666666 
     }, 
     "total_documents": 6 
    }, 
    "4": { 
     "total_interactions": { 
      "value": 701 
     }, 
     "shares": { 
      "value": 300 
     }, 
     "comments": { 
      "value": 0 
     }, 
     "likes": { 
      "value": 401 
     }, 
     "total_documents": 1 
    } 
} 

我希望它是這樣的:

{ 
    "3": { 
     "total_interactions": 493.6666666666667, 
     "shares": 334, 
     "comments": 0, 
     "likes": 159.66666666666666, 
     "total_documents": 6 
    }, 
    "4": { 
     "total_interactions": 701, 
     "shares": 300, 
     "comments": 0, 
     "likes": 401, 
     "total_documents": 1 
    } 
} 

這裏是應該這樣做,但不工作的代碼。沒有影響。不知道什麼是錯的

# the result_hash variable is the first hash with value: <value> 
result_hash.each do |hash_item| 
      hash_item.each do |key,value_hash| 
       if(!value_hash.nil?) 
        value_hash.each do |k,v| 
         hash_item[key] = v 
        end 
       end    
      end 
     end 

回答

1
hash = {"3"=>{"total_documents"=>6, "comments"=>{"value"=>0}, "total_interactions"=>{"value"=>493.667}, "shares"=>{"value"=>334}, "likes"=>{"value"=>159.666666666667}}, 
     "4"=>{"total_documents"=>1, "comments"=>{"value"=>0}, "total_interactions"=>{"value"=>701}, "shares"=>{"value"=>300}, "likes"=>{"value"=>401}}} 

hash.each do |k,v| 
    v.each do |k2, v2| 
    if v2.is_a?(Hash) && v2["value"] 
     hash[k][k2] = v2["value"] 
    end 
    end 
end 

在此之後:

hash = {"3"=>{"total_documents"=>6, "comments"=>0, "total_interactions"=>493.667, "shares"=>334, "likes"=>159.666666666667}, 
     "4"=>{"total_documents"=>1, "comments"=>0, "total_interactions"=>701, "shares"=>300, "likes"=>401}} 
+0

什麼'哈希'在最後?你的意思是你正在迭代的散列是我的散列'result_hash'?對不起,我是ruby的新手 –

+0

爲了解決你的問題中的散列問題,我將它設置爲變量'hash'。然後我在循環結束時再次輸出它,以表明它確實已經改變。我會編輯我的答案,使其更加明顯。 –

+0

謝謝你的作品,我理解它!謝謝! –

2

最大威廉姆斯的代碼是完美的原地。你也可以做到這一點函數式得到一個新的,修正後的哈希:

hash.merge(hash) {|k,v| v.merge(v) {|kk,vv| vv.is_a?(Hash) && vv['value'] ? vv['value'] : vv }} 
+0

謝謝。我給出了這個答案以及Cary的答案,因爲它給了我一個思考問題的新方法。我選擇了Max Williams作爲這個問題的最終答案,因爲他的解決方案使得像我這樣的noob更容易遵循。謝謝! –

+0

一般來說,我認爲最好**不**編輯到位(即使這是我在我的答案)。 –

1

如果您不希望發生變異您最初的哈希,h,你可以這樣做:

h.each_with_object({}) { |(k,v),g| 
    g[k] = v.each_with_object({}) { |(kk,vv),f| 
    f[kk] = (Hash === vv) ? vv[:value] : vv } } 
    #=> {:"3"=>{:total_interactions=>493.667, 
    #   :shares=>334, 
    #   :comments=>0, 
    #   :likes=>159.66666666666666, 
    #   :total_documents=>6}, 
    # :"4"=>{:total_interactions=>701, 
    #   :shares=>300, 
    #   :comments=>0, 
    #   :likes=>401, 
    #   :total_documents=>1}} 
+0

謝謝。我給出了這個答案以及Gene的答案,因爲它給了我一個思考問題的新方法。我選擇了Max Williams作爲這個問題的最終答案,因爲他的解決方案使得像我這樣的noob更容易遵循。謝謝! –