我有一个类似你在本文底部看到的数组(具体来说,它是从fb_graph返回的fb朋友列表)。
如果我想通过这个哈希并从每条记录中选择'identifier'属性,我知道我可以这样做:
@fb_friends.map &:identifier # returns ["123", "134",...]
我的问题是,如果我想从每条记录中挑选出多个属性,该怎么办?基本上我想抓住标识符,名称和图片。天真地我想做点什么:
@fb_friends.map &:identifier, :name, :picture
但当然这不起作用。知道如何解决这个问题吗?
[#<FbGraph::User:0x007f9ec1fd63c8 @identifier="1868", @endpoint="https://graph.facebook.com /----", @access_token="AAAFGzU8vU0gBAEK2q3WYEUJNZAczuO6UPOLqbscwvZALr1gW1ePBqpyarh0uTv6alA2YyNvIzZBcvvIBEYy4i5ulMJZCpWcZD", @cached_collections={}, @name="REMOVED", @first_name=nil, @middle_name=nil, @last_name=nil, @gender=nil, @locale=nil, @languages=[], @link=nil, @username=nil, @third_party_id=nil, @timezone=nil, @verified=nil, @about=nil, @bio=nil, @education=[], @email=nil, @interested_in=[], @political=nil, @favorite_teams=[], @quotes=nil, @relationship_status=nil, @religion=nil, @relationship=nil, @website=nil, @work=[], @sports=[], @favorite_athletes=[], @inspirational_people=[], @mobile_phone=nil, @installed=nil>, #<FbGraph::User:0x007f9ec1fda9f0 @identifier="3??1", @endpoint="https://graph.facebook.com/----", @access_token="AAAFGzU8vU0gBAEK2q3WYEUJNZAczuO6UPOLqbscwvZALr1gW1ePBqpyarh0uTv6alA2YyNvIzZBcvvIBEYy4i5ulMJZCpWcZD", @cached_collections={}, @name="Removed", @first_name=nil, @middle_name=nil, @last_name=nil, @gender=nil, @locale=nil, @languages=[], @link=nil, @username=nil, @third_party_id=nil, @timezone=nil, @verified=nil, @about=nil, @bio=nil, @education=[], @email=nil, @interested_in=[], @political=nil, @favorite_teams=[], @quotes=nil, @relationship_status=nil, @religion=nil, @relationship=nil, @website=nil, @work=[], @sports=[], @favorite_athletes=[], @inspirational_people=[], @mobile_phone=nil, @installed=nil>, #<FbGraph::User:0x007f9ec1fe5030
答案 0 :(得分:11)
这看起来不像嵌套哈希。它看起来像一个FbGraph :: User实例的数组。如果你想将它变成一个哈希数组,每个哈希包含:identifier,:name和:picture,你可以这样做:
@fb_friends.map { |f|
{ identifier: f.identifier, name: f.name, picture: f.picture }
}
答案 1 :(得分:1)
IIRC这将返回一个哈希数组,其中键将是id和一个带有名称和图片的数组的值:
@fb_friends.map{|fbf| [fbf.identifier, [fbf.name, fbf.picture]]}.map{|a| Hash[a]}
{:id1 => ['name', 'picture_url'], :id2 => ['name2', 'picture_url2]}