I tried to filter an array of objects using the .filter()
method and the returned array is either empty or the whole array I was trying to filter. It doesn't show any errors, warnings, or some type of message that indicates something went wrong, so I thought probably I miss something in my logic/knowledge.
Here's the code that I'm trying out:
this.subscription = this.dataService.tasks.subscribe((tasks: Task[]) => {
this.tasks = tasks.filter((task: Task) => {
return task.status === true;
});
});
The dataService.tasks
is a BehaviorSubject<Task[]>
.
For the cases below it returns the whole array of tasks:
return task.status;
return task.status !== true;
return task.status !== false;
return task.status != true;
return task.status != false;
And for these it returns an empty array:
return !task.status;
return task.status === true;
return task.status === false;
return task.status == true;
return task.status == false;
I have also checked the values of the array and it certainly has false and true values as expected.
Here's the complete project i'm working on:
https://github.com/panagiotisplytas/planner
The problem occurs in those two files on the ngOnInit()
method:
src/app/pages/home/home.page.ts
src/app/pages/history/history.page.ts
The capacitor plugin i use for the sqlite database doesn't currently work with electron
or the web
platforms only ios
and android
, so if you want to reproduce the issue you will need to build the app for android
or ios
.
question from:
https://stackoverflow.com/questions/66051772/the-filter-method-returns-the-starting-array-or-an-empty-array 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…