Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
198 views
in Technique[技术] by (71.8m points)

angular - The .filter() method returns the starting array or an empty array

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...