访问哈希数组中的值,其中值是哈希数组
Accessing values in array of hashes where the values are arrays of hashes
我在数组和散列的兔子洞里,请原谅冗长的问题。
我正在尝试在视图中显示值。我通过将散列组合在一起并按值对它们进行分组来创建散列,因此我的数据如下所示:
[
{"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 等的情况下写出输出。以上内容是循环散列数组以及每个散列循环的结果...
您的第一个错误是在哪里抛出的?看起来您需要访问诸如 c["from_number"] 和 c["to_number"].
之类的内容
这可以再重构 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
我在数组和散列的兔子洞里,请原谅冗长的问题。
我正在尝试在视图中显示值。我通过将散列组合在一起并按值对它们进行分组来创建散列,因此我的数据如下所示:
[
{"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 等的情况下写出输出。以上内容是循环散列数组以及每个散列循环的结果...
您的第一个错误是在哪里抛出的?看起来您需要访问诸如 c["from_number"] 和 c["to_number"].
之类的内容这可以再重构 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