【问题标题】:Accessing values in array of hashes where the values are arrays of hashes访问哈希数组中的值,其中值是哈希数组
【发布时间】:2015-05-10 13:33:51
【问题描述】:

我在一个数组和哈希兔子洞中,请原谅冗长的问题。

我正在尝试在视图中显示值。我通过将散列组合在一起并按值对它们进行分组来创建我的散列,因此我的数据如下所示:

[
  {"447478xxxxxx"=>[
                     {:cbrs=>[
                               {"telephone_number"=>"447478xxxxxx", "type"=>"cbr"}
                             ]
                     }, 
                     {:pupil_calls=>"0"}, 
                     {:returned_calls=>"0"}
                    ]
  }, 
  {"447440xxxxxx"=>[
                    {:cbrs=>[
                              {"telephone_number"=>"447440xxxxxx", "type"=>"cbr"}
                            ]
                    }, 
                    {:pupil_calls=>"0"}, 
                    {:returned_calls=>[
                                       {"from_number"=>"447952xxxxxx", "to_number"=>"447440xxxxxx", "type"=>"call", "duration"=>50, "direction"=>"outbound"}, 
                                       {"from_number"=>"447952xxxxxx", "to_number"=>"447440xxxxxx", "type"=>"call", "duration"=>nil, "direction"=>"outbound"}
                                       ]
                    }
                   ] 
  },
  {"447588xxxxxx"=>[
                      {:cbrs=>"0"}, 
                      {:pupil_calls=>[
                                       {"from_number"=>"447588xxxxxx", "to_number"=>"441483xxxxxx", "type"=>"call", "duration"=>5, "direction"=>"inbound"}
                                      ]
                      }, 
                      {:returned_calls=>"0"}
                    ]
  }
]

在我看来,我正在尝试做这种事情

<% array.each do |a| %>

<%= a.first_key %> #this is the number at the start each group eg 447478xxxxxx`

<% a.cbrs.each do |c| %>

   <%=c.type%> #for example, this is just limited sample of the scope of the data

<%end%>

<% a.pupil_calls.each do |c| %>

   <%=c.from_number%> - <%=c.to_number%> 

<%end%>

<% a.returned_calls.each do |c| %>

   <%=c.duration%>

<%end%>

<%end%>

但我不知道如何访问数组中的哈希值中包含的值! (我想我是对的。)

编辑:我所追求的很简单——我只想能够为数组中的每个项目做这样的事情:

 Tel: 447478xxxxxx 
 CBRS: 1 
 Calls: 0
 Returned: 0

 Tel: 447440xxxxxx
 CBRS: 1
 Calls: 0
 Returned Calls: 2 
 Call first returned about 5 minutes after CBR #This would be using created_at dates for example, there is a lot of info I didn't include in my sample data.   
 Returned Call 1: recording link
 Returned Call 2: recording link

希望对您有所帮助,我只是写出了没有 html 等的输出。以上将是循环遍历哈希数组以及每个哈希循环遍历它的结果...

【问题讨论】:

  • 你能给我们想要的输出吗?您已经分享了您的代码,但没有告诉我们它在哪里失败以及您在追求什么。
  • 基本上我的问题包含我上面的伪代码所需的输出,例如,我希望能够循环遍历哈希数组中每个项目的“cbrs”:正如我所示&lt;% array.each do |a| %&gt; &lt;%= a.first_key %&gt; &lt;% a.cbrs.each do |c| &lt;%=c.type%&gt; &lt;%end%&gt; 即输出将是447478xxxxxx cbr
  • 我刚刚编辑了我的问题,让您更好地了解我想要做什么

标签: ruby-on-rails arrays ruby hash


【解决方案1】:

这可以再重构 4 或 5 次,但这应该会得到你想要的:

def val_check(num)
  if num.is_a? Array
    num.size
  else
    num
  end
end


phone_numbers.each do |number|
  number.each do |key, value|
    puts "Tel: #{key}"
    puts "CBRS: #{val_check(value.first[:cbrs])}"
    puts "Calls: #{val_check(value[1][:pupil_calls])}"
    puts "Returned Calls: #{val_check(value[2][:returned_calls])}"
  end
end

输出:

Tel: 447478xxxxxx
CBRS: 1
Calls: 0
Returned Calls: 0
Tel: 447440xxxxxx
CBRS: 1
Calls: 0
Returned Calls: 2
Tel: 447588xxxxxx
CBRS: 0
Calls: 1
Returned Calls: 0

【讨论】:

  • 嘿@Anthony,如果不能保证值的顺序怎么办?说 value.first[:cbrs] - 如果那不是第一个值怎么办?如果您不知道[:cbrs] 出现在哪个顺序中,您将如何编写puts "CBRS: #{val_check(value.first[:cbrs])}" 行?
  • 我可以cbrs = value.find {|x| x[:cbrs] } 然后cbrs.values[0].first["name"] 等等。
【解决方案2】:

您的第一个错误是在哪里抛出的?看起来您需要访问诸如 c["from_number"] 和 c["to_number"] 之类的东西。

【讨论】:

  • 嗯,你使用的是符号和字符串的混合。
  • 我要访问的第一件事是数字,在我使用 的问题中的伪代码中,它实际上是 。我现在遇到的第一个错误是 #<0x007fe2956010b0>
猜你喜欢
  • 2013-03-22
  • 1970-01-01
  • 1970-01-01
  • 2014-10-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-21
  • 2015-06-26
相关资源
最近更新 更多