title: Comparing two arrays of objects using AngularJS
body: <p>I am having an issue with comparing two arrays. One is a fixed data set while the other is being generated dynamically. </p>
  
  <p>A sample of the two arrays are as follows:</p>
  
  <pre><code>// Fixed list of 197 countries
  $scope.countries = [
    {"name": "Afghanistan", "code": "AF"},
    {"name": "Albania", "code": "AF"},
    {"name": "Algeria", "code": "AF"},
  
    //...
  
    {"name": "Zimbabwe", "code": "ZW"}
  ];
  
  //Dynamically generated list of matched countries
  
  $scope.matches = [
    {"name": "Belgium"},
    {"name": "Ghana"}
  ];
  </code></pre>
  
  <p>At the end of the game a function will be run and a comparison of the two arrays will be made. As of right now I have tried (What seems like) almost every combination of this compare using angular.forEach and standard javascript loops. The issue comes when I try to log what countries haven't been matched. </p>
  
  <p>This is the compare function I am running.</p>
  
  <pre><code>$scope.compareArrays = function(){
        angular.forEach($scope.countries, function(country,name){
          angular.forEach($scope.matches, function(match){
            if (country.name !== match.name) {
              console.log(country.name);
            } else {
              console.log("MATCHED");
            }
          });
        });
  };
  </code></pre>
  
  <p>The function will find and log the countries that havent been matched...but it is logging the entire list of unmatched countries multiple times. Specifically once for every object in the "matches" array. </p>
  
  <p>So for example if the matches array is the same as the sample above, it will log the unmatched countries list twice once with Belgium logging as "MATCHED" and the other time logging Belgium as an unmatched country (Same for Ghana but reversed obviously). </p>
  
  <p>I simply want it to log a list of the unmatched countries once and that is all. </p>
  
  <p>I'm hoping it is a simple oversight but cannot figure it out. Thank you in advance.</p>