0

PFB the code of my custom filter where it takes a parameter viewbookoption which is a dropdown value. Based on the dropdown value,the data will be shown in the grid.I implemented forEach loop here but it is not working properly and it keeps traversing the loop and gives me close to 10000 records but actually I have only 10 records which should be displayed in the grid.

Any help in this regard will be much appreciated.

    Controllers.filter('filterUnissued', function() {
     return function(books, viewbookoption) {
       if (viewbookoption == "All Books") {
         return books;
       } else {
         angular.forEach(books, function(book) {
           if (book.issued == false) {
             books.push(book);
           }
        });
      }
      return books;
    }
});
2
  • You are iterating the same array you are pushing into Commented Jun 21, 2016 at 2:20
  • Yeah!! My bad..Thanks for guidance Commented Jun 21, 2016 at 2:31

1 Answer 1

3

You are pushing elements onto the existing books array. Instead, you should create a new array and push onto that. Like this:

var filteredBooks = []
angular.forEach(books, function(book) {
   if (book.issued == false) {
     filteredBooks.push(book);
   }
});
return filteredBooks;

Or, better, use the Array.filter.prototype method:

return books.filter(function(book) {
  return book.issued === false;
});

(Also, always use === instead of ==, unless you have a very good reason to use ==.)

Sign up to request clarification or add additional context in comments.

1 Comment

No problem! Welcome to Stackoverflow. I hope you find it useful here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.