title: Javascript: move objects from one array to another: Best approach?
body: <p>I have two arrays, called 'objects' and 'appliedObjects'.  I'm trying to come up with an elegant way in Javascript and/or Angular to move objects from one array to another. </p>
  
  <p>Initially I did something like this:</p>
  
  <pre><code>   $scope.remove = function () {
          angular.forEach($scope.appliedObjects, function (element, index) {
              if (element.selected) {
                  element.selected = false;
                  $scope.objects.push(element);
                  $scope.appliedObjects.splice(index, 1);
              }
          });
      }
  
     $scope.add= function () {
          angular.forEach($scope.objects, function (element, index) {
              if (element.selected) {
                  element.selected = false;
                  $scope.appliedObjects.push(element);
                  $scope.objects.splice(index, 1);
              }
          });
      }
  </code></pre>
  
  <p>But then I realized that when the value was removed from the looping array, and it would not add or remove every other item, since it went by index.</p>
  
  <p>Then I tried using a temporary array to hold the list of items to be added or removed, and I started getting strange referential issues.</p>
  
  <p>I'm starting to spin a bit on what the best solution to this problem would be...any help and/or guidance would much appreciated.</p>