I have a hash like this
[{"user_id"=>"4672046155508aafb4d01bca27cca8c6", "email"=>"xxxxxxxxxx@gmail.com", "sport"=>[nil]}, {"user_id"=>"007ba6fd74b3ef3c12734ddd0f2280ae", "email"=>"xxxxxxxx@yahoo.com", "sport"=>[nil, nil, nil, nil]}, {"user_id"=>"0085e4d74738a384e10042b62acb56e2", "email"=>"xxxxxxxxxx@gmail.com", "sport"=>[nil, nil, nil]}]
I need a hash with all email in it , How to do this?...whenever I use
users[:email]
It gives the error cannot convert symbol to integer
You can do like this
hash_array = [{"user_id"=>"4672046155508aafb4d01bca27cca8c6", "email"=>"xxxxxxxxxx@gmail.com", "sport"=>[nil]}, {"user_id"=>"007ba6fd74b3ef3c12734ddd0f2280ae", "email"=>"xxxxxxxx@yahoo.com", "sport"=>[nil, nil, nil, nil]}, {"user_id"=>"0085e4d74738a384e10042b62acb56e2", "email"=>"xxxxxxxxxx@gmail.com", "sport"=>[nil, nil, nil]}]
hash_array.map{ |hash| hash["email"] }
It will return an array of all the emails
It isn't hash, it's an array of hashes. To access first hash email
, you can do:
users.first['email']
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.