【问题标题】:Converting several array into separate hashes within one array将多个数组转换为一个数组中的单独哈希
【发布时间】:2013-11-19 10:49:46
【问题描述】:

我需要使用以下数据来构造一个哈希数组。数组中的第一个元素应该是:

 {
   salutation: 'Mr.'
   first_name: 'John',
   last_name: 'Dillinger',
   address: '33 Foolish Lane, Boston MA 02210'
 }

给定的数据如下。我真的很难弄清楚如何做到这一点。一些帮助将不胜感激,因为我目前处于无知状态!!!

 salutations = [
   'Mr.',
   'Mrs.',
   'Mr.',
   'Dr.',
   'Ms.'
 ]

 first_names = [
   'John',
   'Jane',
   'Sam',
   'Louise',
   'Kyle'
 ]

 last_names = [
   'Dillinger',
   'Cook',
   'Livingston',
   'Levinger',
   'Merlotte'
 ]

 addresses = [
   '33 Foolish Lane, Boston MA 02210',
   '45 Cottage Way, Dartmouth, MA 02342',
   "54 Sally's Court, Bridgewater, MA 02324",
   '4534 Broadway, Boston, MA 02110',
   '4231 Cynthia Drive, Raynham, MA 02767'
 ]

我能想出的唯一解决方案不起作用。知道为什么吗???

array_of_hashes = []
array_index = 0

 def array_to_hash (salutations, first_names, last_names, addresses)
   while array_index <= 5
     hash = {}
     hash[salutation] = salutations(array_index)
     hash[first_name] = first_names(array_index)
     hash[last_name] = last_names(array_index)
     hash[address] = addresses(array_index)
     array_of_hashes << hash
     array_index += 1
  end
end

array_to_hash(salutations,first_names,last_names,addresses)

编辑 - 在你们的帮助下,我能够让我的解决方案发挥作用:

 def array_to_hash (salutations, first_names, last_names, addresses)
   array_of_hashes = []
   array_index = 0
   while array_index <= 4
     hash = {}
     hash[:salutation] = salutations[array_index]
     hash[:first_name] = first_names[array_index]
     hash[:last_name] = last_names[array_index]
     hash[:address] = addresses[array_index]
     array_of_hashes << hash
     array_index += 1
   end
   puts array_of_hashes
 end

 array_to_hash(salutations,first_names,last_names,addresses)

【问题讨论】:

    标签: ruby arrays merge hash


    【解决方案1】:
    [salutations, first_names, last_names, addresses].transpose
    .map{|a| Hash[%i[salutation first_name last_name address].zip(a)]}
    

    【讨论】:

    • salutations.zip(first_names, last_names, addresses).map... ? :)
    • @fl00r 你好。这也有效,但我在这里尊重对称性。
    • 感谢@sawa,感谢您的帮助。我自己能想出的唯一解决方案现在也列在问题中。知道为什么这不起作用吗?
    • 因为没有像salutations(array_index)这样的方法。如果您想在索引array_index 处获取saluations 的元素,那么您需要按照Zippie 的答案执行salutations[array_index]
    • 实际上我仍然收到一个错误:“array_to_hash': undefined local variable or method array_index' for main:Object (NameError)”。关于为什么会这样的任何见解?
    【解决方案2】:
    5.times.map do |x|
      {    
       salutation: salutations[x],
       first_name: first_names[x],
       last_name: last_names[x],
       address: addresses[x]
      }
    end
    

    或者如果您不确定每个数组中是否只有五个项目,请将5.times 替换为例如salutations.length.times.

    【讨论】:

    • 感谢 Zippie。在这种情况下,x 是否代表数组 salutations、first_names、last_names 和addresses 中的索引?
    • 是的,x 是索引
    【解决方案3】:
    keys = [:salutations, :first_names, :last_names, :addresses]
    data = [ salutations,  first_names,  last_names,  addresses].transpose
    
    data.map!{|person|  Hash[keys.zip(person)]}
    

    【讨论】:

      【解决方案4】:

      你必须传递你必须传递array_index作为参数,因为你不能访问方法外部的变量。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-07-06
        • 2014-12-10
        • 1970-01-01
        • 1970-01-01
        • 2012-10-29
        • 2019-10-27
        • 2019-05-23
        • 1970-01-01
        相关资源
        最近更新 更多