0

I have an Observable array and I want to filter it by some properties. However, the filtering doesn't working. I tried debugging the code but it's like it doesn't go into the pipe(map(... section.

ngOnInit() {
    this.bookService.getBooks().subscribe(res => {
      this.store.dispatch(listBooks({ books: res }));
    });

    this.filterForm = this.formBuilder.group({
      name: [""],
      author: [""],
      release_year: [""]
    });

    this.filterForm.valueChanges.subscribe(val => {
      this.books$.pipe(map(books => 
        books.filter(b => {
          return b.name.startsWith(val.name) &&
          b.author.startsWith(val.author) &&
          b.release_year.startsWith(val.release_year)
        })
      ))
    })

2 Answers 2

1

this line of your code never execute this.books$.pipe( because you dont subscribe it.any observable need to subscribe to execute.

it is better change your code like below code:

  this.filterForm.valueChanges
  .pipe(
   withLatestFrom(this.books$),
   map(([val,books])>
      books.filter(b => {
        return b.name.startsWith(val.name) &&
        b.author.startsWith(val.author) &&
        b.release_year.startsWith(val.release_year)
      })
   ))
   .subscribe(books => {
    this.books=books;        
   ))  
Sign up to request clarification or add additional context in comments.

Comments

0

This doesn't look like a complete example - e.g. what is this.book$ ?

Additionally your pipe() doesn't seem to end with a subscribe() - and so will not ever do anything

2 Comments

Thank you, subscribe() solved my problem.
Glad to help. Please just accept this as answer - thank you.

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.