简体   繁体   中英

How to iterate over array of hashes, then populate new hash keys using iterated hash values in Ruby

I have an array like this:

result = [
  {:label=>:road, :value=>"carl-schurz str."},
  {:label=>:house_number, :value=>"25"},
  {:label=>:postcode, :value=>"36041"},
  {:label=>:city, :value=>"fulda"},
  {:label=>:state_district, :value=>"fulda kreis"}
] 

I would like to return a hash like the following:

output = {
  "road" => "carl-schurz str.",
  "house_number" => "25",
  "postcode" => "36041",
  "city" => "fulda",
  "state_district" => "fulda kreis"
}

Since I know that hashes can also have positions, I've been trying things like:

result.each do |r|
    r.each do |key, value|
      output[value[0]] = value[1]
    end
   end 

But I'm not getting the correct results..

Just adding some other solutions FYI.

I personally would have done something like this:

Hash[result.map { |h| [h[:label], h[:value]] }]

Another thing you could look into is each_with_object , which can be pretty handy for constructing new objects. In this case that would look something like:

new_hash = result.each_with_object({}) do |h, r|
  r[h[:label]] = h[:value]
end

You can do it easily with "map"...

result.map { |h| [h[:label], h[:value]] }.to_h
Hash[result.map { |h| [h[:label], h[:value]] }]

...or even "reduce"...

result.reduce(Hash.new) { |h,o| h[o[:label]] = o[:value]; h }

This simple benchmark shows that the "reduce" form is slightly faster than the others:

require 'benchmark'

result = [
  {:label=>:road, :value=>"carl-schurz str."},
  {:label=>:house_number, :value=>"25"},
  {:label=>:postcode, :value=>"36041"},
  {:label=>:city, :value=>"fulda"},
  {:label=>:state_district, :value=>"fulda kreis"}
] 

n = 1_000_000

Benchmark.bmbm do |x|
  x.report('Hash[]    ') { n.times { Hash[result.map { |h| [h[:label], h[:value]] }] } }
  x.report('map...to_h') { n.times { result.map { |h| [h[:label], h[:value]] }.to_h } }
  x.report('reduce    ') { n.times { result.reduce(Hash.new) { |h,o| h[o[:label]] = o[:value]; h } } }
end

#                  user     system      total        real
# Hash[]       1.830000   0.040000   1.870000 (  1.882664)
# map...to_h   1.760000   0.040000   1.800000 (  1.810998)
# reduce       1.590000   0.030000   1.620000 (  1.633808) *
result.map { |h| h.values_at(:label, :value) }.to_h
  #=> {:road=>"carl-schurz str.", :house_number=>"25", :postcode=>"36041", 
  #    :city=>"fulda", :state_district=>"fulda kreis"}

另一种方式:

result.map.with_object({}) { |h, new_h| new_h[h[:label]] = h[:value] }

I was able to get the desired result using this:

result.each do |r|
  output[r.values[0]] = values[1]
end

Knowing to use hash_object.values was the key.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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