【问题标题】:Typescript - checking for null and undefined the easy way [duplicate]打字稿 - 以简单的方式检查 null 和 undefined [重复]
【发布时间】:2021-05-23 03:54:37
【问题描述】:

我正在处理一个 Angular 11 项目,我正在努力检查 null 和 undefined。

我有三个字符串 - equipmentIdroomIdpersonnelId 以及一个标志(布尔值)adresseeChosen

如果上述字符串中至少有一个不为空/未定义,我希望adresseeChosen 为真,这是我的代码

this.adresseeChosen= (this.equipmentId != null && this.equipmentId != undefined)  
                      || (this.roomId != null && this.roomId != undefined) 
                      || (this.personnelId != null && this.personnelId != undefined) 

写这个的简短方法是什么?谢谢!

【问题讨论】:

标签: angular typescript


【解决方案1】:

您可以编写一个实用函数来检查值是nullundefined。例如:

export function isNullUndefined(value): boolean {
  return (value === null) || (value === undefined);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-25
    • 2020-07-10
    • 2017-10-03
    • 2021-12-12
    • 1970-01-01
    • 2022-07-20
    • 2018-05-22
    • 1970-01-01
    相关资源
    最近更新 更多