[英]Mapping one hash value to other hash ruby
我有一個 hash 它以下列方式為我提供數據:
details = [{"severity_longevity" => "Medium", "operating_leverage" => "High",
"financial_leverage"=> "Low", "revenue_growth"=> "Low"}]
I have one hash which gives me the score that I am supposed to assign.
分數 = [{"低"=> 5},{"中"=> 10}, {"高"=> 15}]
如何更改details
hash 中的“中”“低”和“高”以及它們的score
hash?
對於哈希,您可以使用transform_values
方法
details = {
"severity_longevity" => "Medium",
"operating_leverage" => "High",
"financial_leverage"=> "Low",
"revenue_growth"=> "Low"
}
score = {"Low" => 5, "Medium" => 10, "High" => 15}
updated = details.transform_values { |v| score[v] }
# => { "severity_longevity" => 10, ... }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.