title: Rails : remove objects from nested array
body: <p>This is a project, where a user has a list a friends, and each friend has their list of friends.
  What I want is a list of common friends between the &quot;selected user&quot; and each of his &quot;friends&quot;, without abusing &quot;each do&quot; loops.</p>
  <pre><code>selected_user = User.all.find_by_username(&quot;Juan&quot;)
  
  data = selected_user.friends.map do |friend|
    friend.friends.select do |friend_from_friend|
      friend_from_friend.id != selected_user.id 
    end
  end
  
  render json: data ,include: [:friends]
  
  ## good piece of data, but Im looking for common friends
  ## the select is not filtering &quot;Juan&quot; as it should ?
  
  [
    {
      &quot;id&quot;: 2,
      &quot;username&quot;: &quot;Matt&quot;,
      &quot;friends&quot;: [
        {
          &quot;id&quot;: 1,
          &quot;username&quot;: &quot;Juan&quot;, &lt;== current_user ( or Juan ) should not be returned
        },
        {
          &quot;id&quot;: 3,
          &quot;username&quot;: &quot;Bill&quot;,
        },
        {
          &quot;id&quot;: 4,
          &quot;username&quot;: &quot;Sophia&quot;, &lt;== Is not one of current_user ( or Juan ) friends.
        }
      ]
    },
    {
      &quot;id&quot;: 3,
      &quot;username&quot;: &quot;Bill&quot;,
      &quot;friends&quot;: [
        {
          &quot;id&quot;: 1,
          &quot;username&quot;: &quot;Juan&quot;, &lt;== current_user ( or Juan ) should not be returned
        },
        {
          &quot;id&quot;: 2,
          &quot;username&quot;: &quot;Matt&quot;,
        },
        {
          &quot;id&quot;: 4,
          &quot;username&quot;: &quot;Sophia&quot;, &lt;== Is not one of current_user ( or Juan ) friends.
        }
      ]
    },
  ]
  </code></pre>
  <p>What I'm looking for a list very similar to this one, with only common friends to Juan.</p>
  <p>The first map seems good to me, it's exactly the chunk of data i'm looking for but then, if only I could remove the entries I dont like.</p>
  <p>I think it's more on the 2nd loop where I lose it.
  Tried with select, filter, find, map, maybe I could share my sql associations.</p>
  <p>I dont know, just correct me if you see anything wrong Im doing.
  Thanks.</p>
  <p><strong>#################### EDIT #######################</strong></p>
  <p>I solved my problem without touching my associations, I'm really happy I went from 7-15 seconds loading time for a 4k lines json file, to only 0.2 seconds.</p>
  <pre><code>## GET USER FRIENDS INCLUDES COMMON FRIENDS
  
    selected_user = User.find_by_username(params[:username])
    data = []
    selected_user_list_of_friends_ids = selected_user.friends.map {|el|el.id}
    current_user_list_of_friends_ids = current_user.friends.map {|el|el.id}
  
     selected_user.friends.map do |friend|
        guy = {owner_id: friend.id, owner_username: friend.username, owner_avatar_link: friend.avatar_link}
        res = friend.friends.filter do |el|
          el.id != current_user.id &amp;&amp;  selected_user_list_of_friends_ids.exclude?(el.id)
        end
        if current_user_list_of_friends_ids.include?(friend.id)
          guy[:users] = res
        else 
          guy[:users] = []
        end
        data.push(guy)
     end
  
     render json: data
  
  </code></pre>