![](/img/trans.png)
[英]How can I utilize a mix of array and hashes in Ruby on Rails Models?
[英]How would I break up and extract an array from an array-of-hashes in Ruby on Rails?
例如:
[ (id=>1, email=>'tim@tim.com', name=>'tim'),
(id=>2, email=>'joe@joe.com', name=>'joe'),
(id=>3, email=>'dan@dan.com', name=>'dan') ]
如何提取電子郵件列並將其放入自己的數組中?
我們打電話給你的陣列users
。 你可以這樣做:
users.map{|u| u[:email]}
這將逐個查看哈希值,將它們稱為u
,提取:email
鍵,然后在新的用戶電子郵件數組中返回結果。
[ {id=>1, email=>'tim@tim.com', name=>'tim'},
{id=>2, email=>'joe@joe.com', name=>'joe'},
{id=>3, email=>'dan@dan.com', name=>'dan'} ].map{|h| h['email']}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.