【问题标题】:Combine 4 arrays to create a hash within a hash组合 4 个数组以在散列中创建散列
【发布时间】:2014-03-16 18:41:34
【问题描述】:

我有以下数组

campaign_names = ["Air Duct Cleaning", "Carpet Cleaning", "Brand Terms", "Upholstery Cleaning"]

impressions = ["358", "1404", "1853", "184"]

clicks = ["3", "6", "53", "0"]

conversions = ["0", "0", "2", "0"]

我想在散列中创建一个散列,以便campaign_names 元素是嵌套散列,然后印象、点击和转换是其中的键,值是每个元素的数组元素。 所需的输出是 -

Hash = {:Air_Duct_Cleaning => { :impressions => "358", :clicks => "3", :conversions => "0" }, :Carpet_Cleaning => { :impressions => "1404", :clicks => "6", :conversions => "0" }, :Brand_Terms => { :impressions => "1853", :clicks => "53", :conversions => "2" }, :Upholstery_Cleaning => { :impressions => "184", :clicks => "0", :conversions => "0" }}

我一直在尝试使用 #zip 来组合它们,然后我希望使用 Hash[] 但最终得到一个空哈希(似乎我只能使用 2 个数组来做到这一点)

zipped = campaign_names.zip(impressions, clicks, conversions)
=> [["Air Duct Cleaning", "358", "3", "0"], ["Carpet Cleaning", "1404", "6", "0"], ["Brand Terms", "1853", "53", "2"], ["Upholstery Cleaning", "184", "0", "0"]]
final_hash = Hash[zipped]
=> {}

然后我尝试使用#each_with_index,但我得到的输出只是我正在调用它的数组

hash = {}
campaign_names.each_with_index { |key, index| hash[key] = values[index] }
=> ["Air Duct Cleaning", "Carpet Cleaning", "Brand Terms", "Upholstery Cleaning"]

【问题讨论】:

    标签: ruby-on-rails ruby arrays hash


    【解决方案1】:
    hash = {}
    campaign_names.each_with_index { |e,i|
        hash[e.gsub(' ','_').to_sym] = {impressions: impressions[i], clicks: clicks[i], conversations: conversions[i]}
    }
    

    【讨论】:

    • 感谢添加_,我忘了我还没有插入那些。效果很好!
    【解决方案2】:
    campaign_names.zip(impressions, clicks, conversions)
    .map{|k, v1, v2, v3| [
      k.gsub(" ", "_").to_sym,
      {impressions: v1, clicks: v2, conversions: v3}
    ]}.to_h
    

    结果:

    {
      :Air_Duct_Cleaning   => {
        :impressions => "358",
        :clicks      => "3",
        :conversions => "0"
      },
      :Carpet_Cleaning     => {
        :impressions => "1404",
        :clicks      => "6",
        :conversions => "0"
      },
      :Brand_Terms         => {
        :impressions => "1853",
        :clicks      => "53",
        :conversions => "2"
      },
      :Upholstery_Cleaning => {
        :impressions => "184",
        :clicks      => "0",
        :conversions => "0"
      }
    }
    

    【讨论】:

    • 谢谢萨瓦!这让我非常接近,但我最终得到一个数组,to_h 导致NoMethodError: undefined method to_h for #<Array:0x45af998> 错误。我正在尝试自己完成它,但恐怕我对v1, v2, v2 不熟悉 - 是那些创建集合吗?
    • 每个都是字符串。如果出现错误,您一定是在使用旧系统。
    【解决方案3】:

    只是为了好玩:

    campaign_names.zip(1..campaign_names.size)
      .map{ |x,y| 
            { x => { impressions: impressions[y-1], 
                     clicks: clicks [y-1], 
                     conversions: conversions[y-1] 
                   } 
            }
      }.inject("merge")
    

    【讨论】:

    • 我喜欢不同的方法,但我更喜欢更接近我所拥有的方法:) - 我实际上可以在其他东西上使用它+1
    【解决方案4】:

    我认为你与 each_with_index 关系很亲密

    这对我有用...

    hash = {}
    campaign_names.each_with_index { |c,index| hash[c] = { impressions: impressions[index],      clicks: clicks[index], conversions: conversions[index] }}
    

    请注意,这将返回campaign_names 数组,但您应该会发现哈希将根据您的需要填充。

    【讨论】:

      【解决方案5】:

      只是为了与众不同,不要使用each_with:

      hash = {}
      campaign_names.length.times do |index| 
          hash[campaign_names[index].gsub(/\s/, '_')] = {
            impressions: impressions[index],
            clicks: clicks[index], 
            conversions: conversions[index]
          } 
      end
      
      > hash
      => {"Air_Duct_Cleaning"=>{:impressions=>"358", :clicks=>"3", :conversions=>"0"},
          "Carpet_Cleaning"=>{:impressions=>"1404", :clicks=>"6", :conversions=>"0"},
          "Brand_Terms"=>{:impressions=>"1853", :clicks=>"53", :conversions=>"2"},
          "Upholstery_Cleaning"=>{:impressions=>"184", :clicks=>"0", :conversions=>"0"}}
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-07-26
        • 1970-01-01
        • 2012-02-21
        • 2015-11-16
        • 2010-12-04
        • 2012-06-12
        • 2013-08-11
        相关资源
        最近更新 更多