Reputation: 781
I have data structure like List< HashMap< String, List>>:
records = [{"1"=>[{"account_id"=>"1", "v"=>"1"}, {"account_id"=>"1", "v"=>"2"}, {"account_id"=>"1", "v"=>"3"}, {"account_id"=>"1", "v"=>"4"]}, {"2"=>[{"account_id"=>"2", "v"=>"4"}, {"account_id"=>"2", "v"=>"4"}, {"account_id"=>"2", "v"=>"4"}]}]
I don't care about the keys in hashmap ("1" and "2" in this case), and want to iterate values of map by group:
records.each do |account_map|
account_record = account_map.values[0] # This line
for i in (0 ... account_record.size - 1)
#do something and update account_record[i]
end
end
end
How can I merge account_record = account_map.values[0]
into each
loop or make it look better. Thanks
Upvotes: 0
Views: 1811
Reputation: 144
Your example is quite confusing, but the regular way to iterate a hash is the following
hash.each do |key, value|
end
So in your example it looks like you should do
records.each do |account_map|
account_map.each do |index, array|
array.each do |hash|
hash['account_id'] # This is how you access your data
hash['v'] # This is how you access your data
end
end
end
Of course you should use better variables names than index, array and hash.
Upvotes: 2