用另一个替换散列中的一个匹配值 [英] Replace one matched value in a hash with another
本文介绍了用另一个替换散列中的一个匹配值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个散列数组:
I have an array of hashes:
arr = [
{:key1=>"one", :key2=>"two", :key3=>"three"},
{:key1=>"four", :key2=>"five", :key3=>"six"},
{:key1=>"seven", :key2=>"eight", :key3=>"nine"}
]
我想用<$ c替换:key1
的值$ c>replacement if :key
= one
。
and I would like to search through and replace the value of :key1
with "replaced"
if :key
= "one"
.
结果数组读取:
The resultant array would then read:
arr = [
{:key1=>"replaced", ;key2=>"two", :key3=>"three"},
{:key1=>"four", ;key2=>"five", :key3=>"six"},
{:key1=>"seven", ;key2=>"eight", :key3=>"nine"}
]
任何人都可以指向正确的方向吗?
Can anyone point me in the right direction?
推荐答案
尝试使用下面的代码来查看它是否解决了您的问题。
Try to use the following code to see if it solves your problem
arr.each { |item| item[:key1] = "replaced" if item[:key1] == "one" }
这篇关于用另一个替换散列中的一个匹配值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文