簡體   English   中英

在特定的 position 將值添加到數組 hash ruby

[英]Adding values to array hash ruby at a specific position

我有一個 hash 響應 object 看起來像這樣:

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}

我必須在每個回復數組對象中添加一個特定 position 的鍵值對,以便響應轉換為類似這樣的東西,為了現在更簡單,讓我們嘗試在特定的 position 添加一個 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}

這是我嘗試過的,我通過jsonResponse運行.each:

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中更新

這是上面代碼片段的output:


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邏輯的工作方式是在我們指定的 position 之前添加,例如它在score 鍵之前添加,是否有我可以編寫的邏輯在指定鍵之后添加到 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
}

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 (又名merge! )和Hash#delete

h.delete('location')

h中刪除鍵值對'location'=>'locX'並返回locX ,之后

h.update('location'=>'locX')

將該鍵值對返回到 hash 的末尾。 這對keys_to_shift中的每個鍵重復。

舉一個簡單的例子:

# 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"}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM