Create address from multiple strings and separate them by comma

Discussão em 'Angular' iniciado por Andurit, Outubro 18, 2024.

  1. Andurit

    Andurit Guest

    let's imagine, I've got 5 strings and any of it can be filled in or just stay empty (it's based on user input)

    I wonder how to separate them nice with comma. I feel like this problem have to be trivial but only idea I found out that will work and is not totally stupid is:

    // Create a function which return me string of arrays
    public getAddress(): string[] {
    let result = [];
    if (this.application.applicant.city) {
    result.push(this.application.applicant.city);
    }
    if (this.application.applicant.postalCode) {
    result.push(this.application.applicant.postalCode);
    }
    if (this.application.applicant.state && this.application.applicant.state.name) {
    result.push(this.application.applicant.state.name);
    }
    return result
    }
    // Then somewhere in ngOnInit() just call this method:
    this.address = this.getAddress();


    And inside of my tempalte:

    <span *ngFor="let item of address; let isLast=last">
    {{item}}{{isLast ? '' : ', '}}
    </span>


    or clasic JS way:

    <span> {{address.join(", ")}} </span>


    And I still feel like this is overcomplicated. Am I missing some easy solution?
    Thanks for any advise

    Continue reading...

Compartilhe esta Página