Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extracting multiple key-value attributes from an array

I have an array like the one you see at the bottom of this post (specifically, it's a list of fb friends returned from fb_graph).

If I wanted to go through this hash and pick out just the 'identifier' attribute from each record, I know I could do something like this:

@fb_friends.map &:identifier  # returns ["123", "134",...]

My question is what if I want to pick out multiple attributes from each record. Basically I want to grab the identifier, name and picture. Naively I want to do something like:

@fb_friends.map &:identifier, :name, :picture

But of course that doesn't work. Any idea how I can pull this off?

[#<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
like image 330
pejmanjohn Avatar asked Dec 15 '22 21:12

pejmanjohn


2 Answers

That doesn't look like a nested hash. It looks like an array of FbGraph::User instances. If you'd like to turn it into an array of hashes, each containing :identifier, :name, and :picture, you could do this:

@fb_friends.map { |f|
  { identifier: f.identifier, name: f.name, picture: f.picture }
}
like image 101
Rob Davis Avatar answered Jan 13 '23 12:01

Rob Davis


IIRC this would return an array of hashes where the key would be the id ad the value an array with the name and picture:

@fb_friends.map{|fbf| [fbf.identifier, [fbf.name, fbf.picture]]}.map{|a| Hash[a]}

{:id1 => ['name', 'picture_url'], :id2 => ['name2', 'picture_url2]}
like image 26
enriclluelles Avatar answered Jan 13 '23 14:01

enriclluelles