ruby 要散列的散列数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10943909/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 05:11:38  来源:igfitidea点击:

Array of hashes to hash

rubyarrayshash

提问by evfwcqcg

For example, I have array of single hashes

例如,我有单个哈希数组

a = [{a: :b}, {c: :d}]

What is best way to convert it into this?

将它转换成这个的最好方法是什么?

{a: :b, c: :d}

回答by Howard

You may use

您可以使用

a.reduce Hash.new, :merge

which directly yields

直接产生

{:a=>:b, :c=>:d}

Note that in case of collisions the order is important. Latter hashes override previous mappings, see e.g.:

请注意,在发生冲突的情况下,顺序很重要。后面的散列覆盖以前的映射,参见例如:

[{a: :b}, {c: :d}, {e: :f, a: :g}].reduce Hash.new, :merge   # {:a=>:g, :c=>:d, :e=>:f}

回答by potashin

You can use .inject:

您可以使用.inject

a.inject(:merge)
#=> {:a=>:b, :c=>:d}

Demonstration

示范

Which initiates a new hash on each iteration from the two merged. To avoid this, you can use destructive :merge!( or :update, which is the same):

它从两个合并的每次迭代中启动一个新的散列。为避免这种情况,您可以使用破坏性:merge!( 或:update,这是相同的):

a.inject(:merge!)
#=> {:a=>:b, :c=>:d}

Demonstration

示范

回答by tokland

These two:

这两个:

total_hash = hs.reduce({}) { |acc_hash, hash| acc_hash.merge(hash) }
total_hash = hs.reduce({}, :merge)

Note that Hash#mergecreates a new hash on each iteration, which may be a problem if you are building a big hash. In that case, use updateinstead:

请注意,Hash#merge每次迭代都会创建一个新的散列,如果您正在构建一个大的散列,这可能是一个问题。在这种情况下,请update改用:

total_hash = hs.reduce({}, :update)

An alternative approach would be to convert the hashes to pairs and then build the final hash:

另一种方法是将散列转换为对,然后构建最终的散列:

total_hash = hs.flat_map(&:to_a).to_h

回答by bigsolom

I came across this answer and I wanted to compare the two options in terms of performance to see which one is better:

我遇到了这个答案,我想在性能方面比较这两个选项,看看哪个更好:

  1. a.reduce Hash.new, :merge
  2. a.inject(:merge)
  1. a.reduce Hash.new, :merge
  2. a.inject(:merge)

using the ruby benchmark module, it turns out that option (2) a.inject(:merge)is faster.

使用 ruby​​ 基准测试模块,结果证明选项 (2)a.inject(:merge)更快。

code used for comparison:

用于比较的代码:

require 'benchmark'

input = [{b: "c"}, {e: "f"}, {h: "i"}, {k: "l"}]
n = 50_000

Benchmark.bm do |benchmark|
  benchmark.report("reduce") do
    n.times do
      input.reduce Hash.new, :merge
    end
  end

  benchmark.report("inject") do
    n.times do
      input.inject(:merge)
    end
  end
end

the results were

结果是

       user     system      total        real
reduce  0.125098   0.003690   0.128788 (  0.129617)
inject  0.078262   0.001439   0.079701 (  0.080383)

回答by Evan Ross

You can transform it to array [[:a, :b]]and after that translate everything to hash {:a=>:b}

您可以将其转换为数组[[:a, :b]],然后将所有内容转换为哈希{:a=>:b}

# it works like [[:a, :b]].to_h => {:a=>:b}

[{a: :b}, {c: :d}].map { |hash| hash.to_a.flatten }.to_h

# => {:a=>:b, :c=>:d}

回答by Rahul Patel

Try this

尝试这个

a.inject({}){|acc, hash| acc.merge(hash)} #=> {:a=>:b, :c=>:d}

回答by Ritesh katare

Just use

只需使用

a.reduce(:merge)
#=> {:a=>:b, :c=>:d}