简体   繁体   中英

Ruby loop/inject data into array with hash keys with different data points

Having a bit of an issue injecting data from 2 sources into the same hash

For example:

 hash = Hash[board_members_ids.map {|x| [x, :name => nil, :title => nil]}]
 hash => {"IQ788201"=>{:name=>nil, :title=>nil},
"IQ534595"=>{:name=>nil, :title=>nil},
"IQ534605"=>{:name=>nil, :title=>nil},
"IQ10049084"=>{:name=>nil, :title=>nil},
"IQ10049098"=>{:name=>nil, :title=>nil},
"IQ24982164"=>{:name=>nil, :title=>nil},
"IQ534637"=>{:name=>nil, :title=>nil},
"IQ534648"=>{:name=>nil, :title=>nil},
"IQ24245395"=>{:name=>nil, :title=>nil},
"IQ145953536"=>{:name=>nil, :title=>nil},
"IQ268369821"=>{:name=>nil, :title=>nil}}

I now have a hash with keys for the ids. Now I have another array that contains data that is in corresponding order that needs to injected for the name of each key.

example:

board_members_names = ["blah1", "blah2", "blah3", "blah4", "blah5", "blah6", "blah7", "blah8", "blah9", "blah10", "blah11"]

I'm fine with using the .store method on the hash but:

I'm trying to loop over the hash and insert each of these "board_member_names" as the value :name value in exact order as the array is given -- and seem to always insert the first value in the array in each of them

Thanks!

hash.each_with_index { |(k,v),i| hash[k][:name] = board_members_names[i] }
  # => {"IQ788201"   =>{:name=>"blah1",  :title=>nil},
  #     "IQ534595"   =>{:name=>"blah2",  :title=>nil},
  #     "IQ534605"   =>{:name=>"blah3",  :title=>nil},
  #     "IQ10049084" =>{:name=>"blah4",  :title=>nil},
  #     "IQ10049098" =>{:name=>"blah5",  :title=>nil},
  #     "IQ24982164" =>{:name=>"blah6",  :title=>nil},
  #     "IQ534637"   =>{:name=>"blah7",  :title=>nil},
  #     "IQ534648"   =>{:name=>"blah8",  :title=>nil},
  #     "IQ24245395" =>{:name=>"blah9",  :title=>nil},
  #     "IQ145953536"=>{:name=>"blah10", :title=>nil},
  #     "IQ268369821"=>{:name=>"blah11", :title=>nil}} 

If the only issue is keeping things in order, this should work.

board_members_names = ["blah1", "blah2", "blah3", "blah4", "blah5", "blah6", "blah7", "blah8", "blah9", "blah10", "blah11"]

hash = {"IQ788201"=>{:name=>nil, :title=>nil},
"IQ534595"=>{:name=>nil, :title=>nil},
"IQ534605"=>{:name=>nil, :title=>nil},
"IQ10049084"=>{:name=>nil, :title=>nil},
"IQ10049098"=>{:name=>nil, :title=>nil},
"IQ24982164"=>{:name=>nil, :title=>nil},
"IQ534637"=>{:name=>nil, :title=>nil},
"IQ534648"=>{:name=>nil, :title=>nil},
"IQ24245395"=>{:name=>nil, :title=>nil},
"IQ145953536"=>{:name=>nil, :title=>nil},
"IQ268369821"=>{:name=>nil, :title=>nil}}

hash.keys.each { |k| hash[k][:name] = board_members_names.shift }

puts hash.to_s

Output:

{"IQ788201"=>{:name=>"blah1", :title=>nil}, "IQ534595"=>{:name=>"blah2", :title=
>nil}, "IQ534605"=>{:name=>"blah3", :title=>nil}, "IQ10049084"=>{:name=>"blah4",
 :title=>nil}, "IQ10049098"=>{:name=>"blah5", :title=>nil}, "IQ24982164"=>{:name
=>"blah6", :title=>nil}, "IQ534637"=>{:name=>"blah7", :title=>nil}, "IQ534648"=>
{:name=>"blah8", :title=>nil}, "IQ24245395"=>{:name=>"blah9", :title=>nil}, "IQ1
45953536"=>{:name=>"blah10", :title=>nil}, "IQ268369821"=>{:name=>"blah11", :tit
le=>nil}}

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