简体   繁体   English

将一个 hash 值映射到另一个 hash ruby

[英]Mapping one hash value to other hash ruby

I have a hash which gives me the data in following manner:我有一个 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.

score = [{"Low"=> 5},{"Medium"=> 10}, {"High"=> 15}]分数 = [{"低"=> 5},{"中"=> 10}, {"高"=> 15}]

How can I change the "Medium" "Low" and "High" in details hash with their number scores from score hash?如何更改details hash 中的“中”“低”和“高”以及它们的score hash?

For Hashes you can use transform_values method对于哈希,您可以使用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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM