在特定位置向数组哈希 ruby 添加值
Adding values to array hash ruby at a specific position
我有一个哈希响应对象,如下所示:
jsonResponse = {:json=>{"reply"=>[{"person"=>"abc", "roll_no"=>"1234", "location"=>"loc1", "score"=>"1"}, {"person"=>"def", "roll_no"=>"1235", "location"=>"loc2", "score"=>"2"},{"person"=>"fgh", "roll_no"=>"1236", "location"=>"loc3", "score"=>"3"}]}, :status=>200}
我必须在每个回复数组对象的特定位置添加一个键值对,这样回复就变成了这样的东西,使它更简单现在,让我们尝试在特定位置添加一个 samke 键值对:
jsonResponse = {:json=>{"reply"=>[{"person"=>"abc", "roll_no"=>"1234","location"=>"loc1", "new_value => "new_result", "score"=>"1"}, {"person"=>"def", "roll_no"=>"1235", "location"=>"loc2","new_value => "new_result", "score"=>"2"},{"person"=>"fgh", "roll_no"=>"1236", "location"=>"loc3", "new_value => "new_result", "score"=>"3"}]}, :status=>200}
这是我尝试过的方法,我 运行 .each through jsonResponse :
jsonResponse[:json]['reply'].each do |object|
objectArray = object.to_a
insert_at = objectArray.index(objectArray.assoc('score'))
object = Hash[objectArray.insert(insert_at, ['new_value','new_result'])]
print("\n\nTest\n\n")
print object
end
print("\n\nFinal Response\n\n")
print jsonResponse
我正在打印的 object 具有所需的响应,但它没有在 jsonResponse
这是上述代码片段的输出:
Test
{"person"=>"abc", "roll_no"=>"1234", "location"=>"loc1", "new_value"=>"new_result", "score"=>"1"}
Test
{"person"=>"def", "roll_no"=>"1235", "location"=>"loc2", "new_value"=>"new_result", "score"=>"2"}
Test
{"person"=>"fgh", "roll_no"=>"1236", "location"=>"loc3", "new_value"=>"new_result", "score"=>"3"}
Final Response
{:json=>{"reply"=>[{"person"=>"abc", "roll_no"=>"1234", "location"=>"loc1", "score"=>"1"}, {"person"=>"def", "roll_no"=>"1235", "location"=>"loc2", "score"=>"2"}, {"person"=>"fgh", "roll_no"=>"1236", "location"=>"loc3", "score"=>"3"}]}, :status=>200}
Q2. 从代码片段中也可以看出 insert_at 逻辑的工作方式是在我们指定的位置之前添加,例如,它在 得分键 之前添加,是否有我可以编写的逻辑添加到 在指定键之后而不是之前的位置?
感谢大家的努力
举个简单的例子:
# define order
order = [:first, :second, :third]
# the hash you want to order by
json = { :third=>"3", :second=>2, :first=>1 }
result = json.sort_by { |k, _| order.index(k) }.to_h
那么结果将是 {:first=>1, :second=>2, :third=>"3"}
我们得到了三个对象。
jsonResponse = {
:json=>{
"reply"=>[
{"person"=>"abc", "roll_no"=>"1234", "location"=>"loc1", "score"=>"1"},
{"person"=>"def", "roll_no"=>"1235", "location"=>"loc2", "score"=>"2"},
{"person"=>"fgh", "roll_no"=>"1236", "location"=>"loc3", "score"=>"3"}
]
},
:status=>200
}
key_value_pair_to_add = { 'new_value'=>'new_result' }
key_to_precede = 'location'
然后我们修改jsonResponse
如下。
keys_to_shift = jsonResponse[:json]['reply'][0].keys.
drop_while { |k| k != key_to_precede }
#=> ["location", "score"]
jsonResponse[:json]['reply'].each do |h|
h.update('new_value'=>'new_result')
keys_to_shift.each { |k| h.update(k=>h.delete(k)) }
end
jsonResponse
#=> {
# :json=>{
# "reply"=>[
# {"person"=>"abc", "roll_no"=>"1234", "new_value"=>"new_result",
# "location"=>"loc1", "score"=>"1"},
# {"person"=>"def", "roll_no"=>"1235", "new_value"=>"new_result",
# "location"=>"loc2", "score"=>"2"},
# {"person"=>"fgh", "roll_no"=>"1236", "new_value"=>"new_result",
# "location"=>"loc3", "score"=>"3"}
# ]
# },
# :status=>200
# }
见Hash#update (aka merge!
) and Hash#delete。
h.delete('location')
从 h
和 returns locX
中删除 key-value 对 'location'=>'locX'
,之后
h.update('location'=>'locX')
returns 即 key-value 对到散列的末尾。对 keys_to_shift
.
中的每个键重复此操作
我有一个哈希响应对象,如下所示:
jsonResponse = {:json=>{"reply"=>[{"person"=>"abc", "roll_no"=>"1234", "location"=>"loc1", "score"=>"1"}, {"person"=>"def", "roll_no"=>"1235", "location"=>"loc2", "score"=>"2"},{"person"=>"fgh", "roll_no"=>"1236", "location"=>"loc3", "score"=>"3"}]}, :status=>200}
我必须在每个回复数组对象的特定位置添加一个键值对,这样回复就变成了这样的东西,使它更简单现在,让我们尝试在特定位置添加一个 samke 键值对:
jsonResponse = {:json=>{"reply"=>[{"person"=>"abc", "roll_no"=>"1234","location"=>"loc1", "new_value => "new_result", "score"=>"1"}, {"person"=>"def", "roll_no"=>"1235", "location"=>"loc2","new_value => "new_result", "score"=>"2"},{"person"=>"fgh", "roll_no"=>"1236", "location"=>"loc3", "new_value => "new_result", "score"=>"3"}]}, :status=>200}
这是我尝试过的方法,我 运行 .each through jsonResponse :
jsonResponse[:json]['reply'].each do |object|
objectArray = object.to_a
insert_at = objectArray.index(objectArray.assoc('score'))
object = Hash[objectArray.insert(insert_at, ['new_value','new_result'])]
print("\n\nTest\n\n")
print object
end
print("\n\nFinal Response\n\n")
print jsonResponse
我正在打印的 object 具有所需的响应,但它没有在 jsonResponse
这是上述代码片段的输出:
Test
{"person"=>"abc", "roll_no"=>"1234", "location"=>"loc1", "new_value"=>"new_result", "score"=>"1"}
Test
{"person"=>"def", "roll_no"=>"1235", "location"=>"loc2", "new_value"=>"new_result", "score"=>"2"}
Test
{"person"=>"fgh", "roll_no"=>"1236", "location"=>"loc3", "new_value"=>"new_result", "score"=>"3"}
Final Response
{:json=>{"reply"=>[{"person"=>"abc", "roll_no"=>"1234", "location"=>"loc1", "score"=>"1"}, {"person"=>"def", "roll_no"=>"1235", "location"=>"loc2", "score"=>"2"}, {"person"=>"fgh", "roll_no"=>"1236", "location"=>"loc3", "score"=>"3"}]}, :status=>200}
Q2. 从代码片段中也可以看出 insert_at 逻辑的工作方式是在我们指定的位置之前添加,例如,它在 得分键 之前添加,是否有我可以编写的逻辑添加到 在指定键之后而不是之前的位置?
感谢大家的努力
举个简单的例子:
# define order
order = [:first, :second, :third]
# the hash you want to order by
json = { :third=>"3", :second=>2, :first=>1 }
result = json.sort_by { |k, _| order.index(k) }.to_h
那么结果将是 {:first=>1, :second=>2, :third=>"3"}
我们得到了三个对象。
jsonResponse = {
:json=>{
"reply"=>[
{"person"=>"abc", "roll_no"=>"1234", "location"=>"loc1", "score"=>"1"},
{"person"=>"def", "roll_no"=>"1235", "location"=>"loc2", "score"=>"2"},
{"person"=>"fgh", "roll_no"=>"1236", "location"=>"loc3", "score"=>"3"}
]
},
:status=>200
}
key_value_pair_to_add = { 'new_value'=>'new_result' }
key_to_precede = 'location'
然后我们修改jsonResponse
如下。
keys_to_shift = jsonResponse[:json]['reply'][0].keys.
drop_while { |k| k != key_to_precede }
#=> ["location", "score"]
jsonResponse[:json]['reply'].each do |h|
h.update('new_value'=>'new_result')
keys_to_shift.each { |k| h.update(k=>h.delete(k)) }
end
jsonResponse
#=> {
# :json=>{
# "reply"=>[
# {"person"=>"abc", "roll_no"=>"1234", "new_value"=>"new_result",
# "location"=>"loc1", "score"=>"1"},
# {"person"=>"def", "roll_no"=>"1235", "new_value"=>"new_result",
# "location"=>"loc2", "score"=>"2"},
# {"person"=>"fgh", "roll_no"=>"1236", "new_value"=>"new_result",
# "location"=>"loc3", "score"=>"3"}
# ]
# },
# :status=>200
# }
见Hash#update (aka merge!
) and Hash#delete。
h.delete('location')
从 h
和 returns locX
中删除 key-value 对 'location'=>'locX'
,之后
h.update('location'=>'locX')
returns 即 key-value 对到散列的末尾。对 keys_to_shift
.