I have the following hash:
doc = {"house"=>{"Team 1"=>0, "Team 2"=>0, "Team 3"=>0, "Team 4"=>0}}
I am trying to print the "Team" keys.
For example:
Team 1
Team 2
Team 3
Team 4
I tried using doc.values
but this returns an array [{"Team 1"=>0, "Team 2"=>0, "Team 3"=>0, "Team 4"=>0}]
. Which means I can't do doc.values.keys
.
I tried using a for loop, but somehow it prints the value this way instead:
Team 1
0
Team 2
0
..
Is there a way to to get these keys?
This should do it:
puts doc["house"].keys
If you don't necessarily know what the first key will be, this also works:
puts doc.values.flat_map(&:keys)
Just for fun, here's a third approach, if you know there'll always be just the one outer key:
puts doc[doc.keys.first].keys
That what you're after? Hope it helps - let me know if you've any questions!
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.