title: Comparing objects between 2 arrays in Angularjs
body: <p>I'm trying to figure the best way to do this, given that there will be a lot of data and I don't want to have giant loops hogging up resources. I have a lot of regular javascript/jquery experience, but am new to Angular and want to do it with Angularjs best practices.</p>
  
  <p>I have a list of <code>items</code> and users can "favorite" them. When viewing all <code>items</code>, I'd like to show the ones that favorited by marking them with an indicator. Both <code>items</code> and <code>favorites</code> are in separate tables and returned as JSON with different IDs.</p>
  
  <p>JS:</p>
  
  <pre><code>$scope.favorites = [];//$http.get of JSON file
  $scope.items = [];//$http.get of JSON file
  
  $scope.isFavorite = function(itemId) {
      var len = $scope.favorites. !== 'undefined' ? $scope.favorites.length : 0;
      for(var i = 0; i &lt; len; i++) {
          if($scope.favorites[i].id === itemId) {
              return true;
          }
      }
  };
  </code></pre>
  
  <p>HTML:</p>
  
  <pre><code>&lt;div ng-repeat="item in items"&gt;
      &lt;i ng-class="isFavorite(item.id) ? 'icon-bookmark' : 'icon-bookmark-empty'"&gt;&lt;/i&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>I have a couple other checks for "<code>isFavorite</code>" so I'm trying to figure the best way to compare the 2 without checking every time in a loop. Any suggestions?</p>