Angular. How can I filter dynamic array in controller?
<p>could someone help me with issue?nf n I have array of objects which is displayed in table and I have search. Every object is one row in table. The main problem is in array. We can modify it at any time (can add new rows, delete existing rows and change value in table) , even if we search something. </p>

<p>Now I have something like this:</p>

<pre><code>$scope.$watch( 'search', function() {
   if($scope.search!== "") {
       if(! $scope.initArray.length) {
             $scope.initArray= $scope.array;
       }
       $scope.array= $filter('filter')($scope.initArray, function(item) {
             return item.name1.indexOf($scope.search) &gt; -1 ||
                    item.name2.indexOf($scope.search) &gt; -1 ||
                    item.name3.toLowerCase().indexOf($scope.search) &gt; -1;
       });
   } else {
       $scope.array= $scope.initArray;
   } 
 });
</code></pre>

<p>As you can see, I use two arrays. Everything is good, but when I want to change $scope.array I have to change $scope.initArray. And it causes a lot of issues.</p>

<p>For example, table has 3 rows. Every row consist of 3 colomns. I search something and it finds just one row (search has to find value at least in one of colomn). After that I add new row. It displays in table if it contains value I'm searching for. If we clear search field, all data is displayed back. For this correct behavior, I have to do a lot of equal manipulation with $scope.initArray and $scope.array. If I use just one array, after searching table contains incorrect data.</p>

<p>Is there a way where I can use just one array? </p>

<p>$scope.array I pass it to UI. </p>

<p>$scope.initArray it's initial data (before search)</p>