Home > Software engineering >  how to avoid when the data is an empty array
how to avoid when the data is an empty array

Time:09-26

I have this JSON:

{
    "id": 43,
    "dataEvento": "2022-09-01T00:00:00.000 0000",
    "dataInvio": null,
    "idComunicazioneAssociata": null,
    "certificatoMedico": []
}

I'm using a condition on template but doens't work.

            <ng-container *ngIf="segnalazione.certificatoMedico !== []">
             .............
            </ng-container>

I want to show this ng-container only when I have data in certificatoMedico

Any ideas please how to do this? Because that's not how it works for me.

Thanks

CodePudding user response:

Check the length !

*ngIf="segnalazione.certificatoMedico.length !== 0"

CodePudding user response:

Yes, javascript is a weird language, use .length to check if an array is empty <ng-container *ngIf="segnalazione.certificatoMedico.length > 0">

There is no simple way to compare arrays https://www.w3docs.com/snippets/javascript/how-to-compare-two-javascrpt-arrays.html

!== is checking if it's the same object

CodePudding user response:

Check the length, but do it the smart way :

<ng-container *ngIf="!segnalazione?.certificatoMedico?.length">
  • Related