1

I get data from backend with this function

  private loaddata(): void {
    this.dataService.loaddata().pipe(
      tap((response: any) => {
        this.persons = response.results;
        this.personmembertrue = this.persons.filter(x => x.is_family_member === 'false')
      }),
      takeUntil(this.onComponentDestroy)
    ).subscribe();
  }

and console.log(response) show JSON like below

{
    "count": 38,
    "next": null,
    "previous": null,
    "results": [
        {
            "id": 113,
            "foreigner": false,
            "outside_community": false,
            "nipt": "",
            "nid": "G45675570K",
            "is_family_member": true
        },
        {
            "id": 115,
            "foreigner": false,
            "outside_community": false,
            "nipt": "",
            "nid": "K30776771A",
            "is_family_member": false
        },
        {
            "id": 116,
            "foreigner": false,
            "outside_community": false,
            "nipt": "",
            "nid": "J305070577",
            "is_family_member": false
        }...
      ]
    }

What I want are data that have "is_family_member": false, for this I create this.personmembertrue = this.persons.filter(x => x.is_family_member === 'false')

this part of code show empty.

Any idea please how to show data with "is_family_member": false

1
  • The string 'false' is truthy while the boolean false is not. You don't need the equality check in the condition: this.personmembertrue = this.persons.filter(x => !x.is_family_member). Commented Mar 19, 2021 at 14:57

3 Answers 3

2

Change the condition to:

this.personmembertrue = this.persons.filter(x => x.is_family_member === false);

See if that works.

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

Comments

0

Try using only ==, with 3 = you compare type and value, or try removing quotes, probably you are comparing bool with string.

Comments

0

We can even simplify @Zunayed Shahriar expression with that

this.personmembertrue = this.persons.filter(x => !x.is_family_member);

EDITED: It will only work if is_family_member is always either true/false as mentionned @Zunayed Shahriar in comment.

2 Comments

It will also work. But if the field is nullable and is null then the data will also be filtered. If the user only wants data where the field is not null and false then this expression won't be accurate. Right?
Yes you are right :) I edited my answer thanks

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.