Sha*_*ila 0 ruby ruby-on-rails ruby-on-rails-4
我必须将弹性搜索结果解析为特定格式。为此,我需要将搜索结果哈希值放入数组中,我有这个:
hash = {
"ABC": {
"attributes": {
"id": "1",
"from": "test",
"to": "something",
}
},
"XYZ": {
"attributes": {
"id": "1",
"from": "value",
"to": "another value",
}
}
}
Run Code Online (Sandbox Code Playgroud)
我想解决这个问题:
"ABC": [
{
"attributes": {
"id": "1",
"from": "test",
"to": "something",
}
}],
"XYZ": [
{
"attributes": {
"id": "1",
"from": "value",
"to": "another value",
}
}
]
Run Code Online (Sandbox Code Playgroud)
简单地说,哈希值应该是数组。请有人指导我。
输入
hash = {
"ABC": {
"attributes": {
"id": "1",
"from": "test",
"to": "something",
}
},
"XYZ": {
"attributes": {
"id": "1",
"from": "value",
"to": "another value",
}
}
}
Run Code Online (Sandbox Code Playgroud)
代码
p hash.transform_values { |value| [value] }
Run Code Online (Sandbox Code Playgroud)
输出
{:ABC=>[{:attributes=>{:id=>"1", :from=>"test", :to=>"something"}}], :XYZ=>[{:attributes=>{:id=>"1", :from=>"value", :to=>"another value"}}]}
Run Code Online (Sandbox Code Playgroud)