【问题标题】:Hash modification issue in hash, replacing value in ruby哈希中的哈希修改问题,替换红宝石中的值
【发布时间】:2015-12-03 04:15:09
【问题描述】:

我想去掉散列中每个属性内的 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

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-3 hash


    【解决方案1】:

    Max Williams 的代码非常适合就地使用。你也可以用函数式的方式来获得一个新的、更正的哈希:

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

    【讨论】:

    • 谢谢。我给出了这个答案以及 Cary 的答案,因为它为我提供了一种思考问题的新方法。我选择 Max Williams 作为这个问题的最终答案,因为他的解决方案让像我这样的菜鸟更容易理解。谢谢!
    • 一般来说,我认为最好就地编辑(尽管我在回答中就是这样做的)。
    【解决方案2】:
    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}}
    

    【讨论】:

    • 最后的hash 是什么?你的意思是你正在迭代的哈希是我的哈希result_hash?抱歉,我是 ruby​​ 新手
    • 为了处理问题中的哈希,我将其设置为变量hash。然后我在循环结束时再次输出它只是为了表明它确实已经改变了。我将编辑我的答案以使其更加明显。
    • 谢谢你的作品,我明白了!谢谢!
    【解决方案3】:

    如果你不想改变你的初始哈希,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}} 
    

    【讨论】:

    • 谢谢。我给出了这个答案以及 Gene 的答案,因为它为我提供了一种思考问题的新方法。我选择 Max Williams 作为这个问题的最终答案,因为他的解决方案让像我这样的菜鸟更容易理解。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2011-07-10
    • 1970-01-01
    • 2011-08-10
    • 2012-12-19
    • 2012-10-17
    • 1970-01-01
    • 1970-01-01
    • 2019-12-24
    相关资源
    最近更新 更多