在 Ruby 中重新映射哈希的最佳方法是什么?

发布于 2024-07-17 14:31:43 字数 439 浏览 8 评论 0原文

是否有一种简单的方法可以通过以下方式重新映射 ruby​​ 中的哈希值:

from:

{:name => "foo", :value => "bar"}

to:

{"foo" => "bar"}

最好是在迭代此类哈希值的数组时使执行此操作变得简单的方式:

from:

[{:name => "foo", :value => "bar"}, {:name => "foo2", :value => "bar2"}]

to:

{"foo" => "bar", "foo2" => "bar2"}

谢谢。

Is there a simple way of remapping a hash in ruby the following way:

from:

{:name => "foo", :value => "bar"}

to:

{"foo" => "bar"}

Preferably in a way that makes it simple to do this operation while iterating over an array of this type of hashes:

from:

[{:name => "foo", :value => "bar"}, {:name => "foo2", :value => "bar2"}]

to:

{"foo" => "bar", "foo2" => "bar2"}

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

拔了角的鹿 2024-07-24 14:31:43
arr = [ {:name=>"foo", :value=>"bar"}, {:name=>"foo2", :value=>"bar2"}]

result = {}
arr.each{|x| result[x[:name]] = x[:value]}

# result is now {"foo2"=>"bar2", "foo"=>"bar"}
arr = [ {:name=>"foo", :value=>"bar"}, {:name=>"foo2", :value=>"bar2"}]

result = {}
arr.each{|x| result[x[:name]] = x[:value]}

# result is now {"foo2"=>"bar2", "foo"=>"bar"}
两相知 2024-07-24 14:31:43

Vanson Samuel 代码的修改版本达到了预期目的。
这是一篇单行诗,但相当长。

arr = [{:name=>"foo", :value=>"bar"}, {:name=>"foo2", :value=>"bar2"}]

arr.inject({}){|r,item| r.merge({item['name'] => item['value']})}

# result {"foo" => "bar", "foo2" => "bar2"}

不过,我不会说它比 Gishu 的版本更漂亮。

A modified version of Vanson Samuel's code does the intended.
It's a one-liner, but quite a long one.

arr = [{:name=>"foo", :value=>"bar"}, {:name=>"foo2", :value=>"bar2"}]

arr.inject({}){|r,item| r.merge({item['name'] => item['value']})}

# result {"foo" => "bar", "foo2" => "bar2"}

I wouldn't say that it's prettier than Gishu's version, though.

心清如水 2024-07-24 14:31:43

作为一般经验法则,如果您有 {:name => 形式的哈希值 “foo”,:值=> "bar"},通常最好使用 ["foo", "bar"] 元组。

arr = [["foo", "bar"], ["foo2", "bar2"]]
arr.inject({}) { |accu, (key, value)| accu[key] = value; accu }

As a general rule of thumb, if you have a hash of the form {:name => "foo", :value => "bar"}, you're usually better off with using a tuple of ["foo", "bar"].

arr = [["foo", "bar"], ["foo2", "bar2"]]
arr.inject({}) { |accu, (key, value)| accu[key] = value; accu }
梦毁影碎の 2024-07-24 14:31:43

我知道这很旧,但实现此目的的最巧妙方法是将哈希数组映射到元组数组,然后使用 Hash[] 从中构建哈希,如下所示:

arr = [{:name => "foo", :value => "bar"}, {:name => "foo2", :value => "bar2"}]
Hash[ array.map { |item| [ item[:name], item[:value] ] } ]

# => {"foo"=>"bar", "foo2"=>"bar2"}

I know this is old, but the neatest way to achieve this is to map the array of hashes to an array of tuples, then use Hash[] to build a hash from that, as follows:

arr = [{:name => "foo", :value => "bar"}, {:name => "foo2", :value => "bar2"}]
Hash[ array.map { |item| [ item[:name], item[:value] ] } ]

# => {"foo"=>"bar", "foo2"=>"bar2"}
悲念泪 2024-07-24 14:31:43

有点晚了但是:

[{ name: "foo", value: "bar" },{name: "foo2", value: "bar2"}].map{ |k| k.values }.to_h

a bit late but:

[{ name: "foo", value: "bar" },{name: "foo2", value: "bar2"}].map{ |k| k.values }.to_h
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文