Bart05z
Bart05z

Reputation: 33

Angular 2 ES5 filter with TS

I cannot make ES5 filter function works with Angular 2 (with TypeScript).

My filter function looks like this:

getApplicableNotes(): void {
this.noteService
  .getNotes()
  .then(notes => {
    notes.filter((note) => !note._deleted && !note._done);
    this.notes = notes;
  })
  .catch((error) => this.error = error);
}

My TypeScript class is extremely straightforward:

export class Note {
  id: number;
  title: string;
  description: string;
  _starred = false;
  _done = false;
  _deleted = false;

  constructor(id: number, title: string, description: string, starred?: boolean, done?: boolean, deleted?: boolean) {
    this.id = id;
    this.title = title;
    this.description = description;
    this._starred = starred ? starred : false;
    this._done = done ? done : false;
    this._deleted = deleted ? deleted : false;
  };

}

Although, my notes array is never filtered, no matter what properties I will set up in Note constructor.

Upvotes: 0

Views: 113

Answers (1)

Michał Perłakowski
Michał Perłakowski

Reputation: 92521

The filter() method doesn't modify the array, but returns a new filtered array. You should assign the result to this.notes:

.then(notes => {
  this.notes = notes.filter((note) => !note._deleted && !note._done);
})

Upvotes: 2

Related Questions