【发布时间】:2020-06-11 20:29:17
【问题描述】:
我的数据是这样的:
h = { themes_data: {
Marketing: [
{
id: 68,
projectno: "15",
}
],
Produktentwicklung: [
{
id: 68,
projectno: "15",
},
{
id: 4,
projectno: "3",
}
],
Marketing_summary: [
{
ges: {
result: "47.6"
},
theme: "Marketing"
}
],
Produktentwicklung_summary: [
{
ges: {
result: "87.7"
},
theme: "Produktentwicklung"
}
]
}
}
我的输出应该是这样的:
{ "marketing" => [
{
id: 68,
projectno: "15",
},
{
ges: {
result: "47.6"
},
theme: "Marketing"
}
],
"Produktentwicklung" => [
{
id: 68,
projectno: "15"
},
{
id: 4,
projectno: "3",
},
{
ges: {
result: "87.7"
},
theme: "Produktentwicklung"
}
]
}
代码:
def year_overview_theme
branch_hash = {}
@themes_data.each do |td|
arr = []
td[1].map do |dt|
arr << [{content: dt[:projectno], size: 5, align: :right, background_color: 'D8E5FF'}]
end
branch_hash["#{td[0]}"] = arr
end
branch_hash
end
问题是它没有迭代正确的哈希键。
例如,我想喜欢:
marketing + marketing_summary 为 1 个哈希值,类似
Produktentwicklung = Produktentwicklung_summary 作为一个哈希,但我的逻辑存在一些问题。
有没有一种方法可以在 2 次迭代后进行检查,
它应该做 arr
【问题讨论】:
-
我编辑只是为了更正输入哈希并重新格式化输出哈希。
标签: ruby-on-rails ruby hash-of-hashes