用另一个替换散列中的一个匹配值
问题描述:
我有一个散列数组:
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"}
]
我想用: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" }