簡體   English   中英

打字稿和角度-格式字符串

[英]Typescript & Angular - Format String

Typescript \\ Angular的新手,想知道它們是否是格式化字符串\\刪除空字符串的簡便方法? 因此,如果鎮是空字符串,則不會顯示“ NULL”

this.client = c;
this.clientFulladdress= `${this.client.Address}, ${this.client.Town}, ${this.client.County}, ${this.client.PostCode}` 

數組函數非常方便,例如:

let fields = [this.client.Address, this.client.Town, this.client.County, this.client.PostCode];

this.clientFulladdress = fields.filter((field) => field).join(", ");

這將篩選出任何空字符串或空字符串,然后將其余的每個字符串用逗號連接

 this.client = c;
 this.clientFulladdress= `${this.client.Address || ""}, ${this.client.Town || "" }, ${this.client.County || ""}, ${this.client.PostCode || ""}`;

這是您達到要求的方式。

如果您不想多次使用this.client ,也可以執行以下操作

this.client = c;
let {Address, Town, Country, PostCode} = this.client;
this.clientFulladdress = `${Address || ""}, ${Town || ""}, ${Country || ""}, ${PostCode || ""}`;

根據評論其他方式也可以

this.client = c;
let {Address, Town, Country, PostCode} = this.client;
this.clientFulladdress = `${Address || ""}${Town ? ", " + Town : ""}${Country ? ", " + Country : ""}${PostCode ? ", " + PostCode : ""}`;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM