组合4个数组以在散列中创建散列

时间:2014-03-16 04:05:39

标签: ruby-on-rails ruby arrays hash

我有以下数组

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个数组来做)< / p>

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"]

5 个答案:

答案 0 :(得分:1)

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"
  }
}

答案 1 :(得分:1)

hash = {}
campaign_names.each_with_index { |e,i|
    hash[e.gsub(' ','_').to_sym] = {impressions: impressions[i], clicks: clicks[i], conversations: conversions[i]}
}

答案 2 :(得分:1)

只是为了好玩:

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")

答案 3 :(得分:0)

我认为你和each_with_index非常接近

这对我有用......

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

请注意,这会返回campaign_names数组,但您应该会根据需要找到哈希值。

答案 4 :(得分:0)

只是为了与众不同而不是使用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"}}