javascript - Ionic - 检查 Typescript 中的 ""

标签 javascript typescript ionic-framework

我对 Ionic 和 Typescript 还很陌生,并且收到了一个应用程序的缺陷,其中出现了订单的 shell,但数据全是空白。 我的代码:

// Loop over MONITORS
this.monitorArrayLength = this.detail.orderDetailList[i].monitors.length;
for (let j = 0; j < this.monitorArrayLength; j++) {
  this.monitorArray[j] = this.detail.orderDetailList[i].monitors[j];

  if (
    this.monitorArray[j].serialNum !== null ||
    this.monitorArray[j].serialNum !== "" ||
    this.monitorArray[j].status !== null ||
    this.monitorArray[j].status !== "" ||
    this.monitorArray[j].brandModel !== null ||
    this.monitorArray[j].brandModel !== "" ||
    this.monitorArray[j].docId !== null ||
    this.monitorArray[j].docId !== ""
  ) {
    this.showMonitorArray[j] = true; // Show monitor

    if (this.monitorArray[j].docId !== null) {
      this.pdfIconArray[j] = true; // Show pdf icon
    } else {
      this.pdfIconArray[j] = false; // Show normal icon
    }
  } else {
    this.showMonitorArray[j] = false; // Don't show monitor
  }
}

因此,如果从数据库带回的结果为 null 或“”,则基本上不会出现任何内容。

这是我从数据库返回的结果:

monitors: [{ serialNum: "", status: "", brandModel: "", docId: null }]

我上面的代码没有正确检查“”,并且数据的外壳在不应该显示的时候仍然显示。

这是检查“”的正确语法吗?

如有任何帮助,我们将不胜感激!

最佳答案

这与 Ionic 或 TypeScript 无关。您应该将 if 条件更改为

serialNum is defined AND status is defined AND brandModel is defined AND docId is defined

其中“已定义”表示非 null""

if (
  (this.monitorArray[j].serialNum !== null &&
    this.monitorArray[j].serialNum !== "") &&
  (this.monitorArray[j].status !== null &&
    this.monitorArray[j].status !== "") &&
  (this.monitorArray[j].brandModel !== null &&
    this.monitorArray[j].brandModel !== "") &&
  (this.monitorArray[j].docId !== null && this.monitorArray[j].docId !== "")
) {
  //...
}

但是看起来真的很乱。您可以将其缩短为函数

function isDefined(monitor) {
  const { serialNum, status, brandModel, docId } = monitor;
  return serialNum && status && brandModel && docId;
}

确保所有这些值不是 falsy .

关于javascript - Ionic - 检查 Typescript 中的 "",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58690467/

相关文章:

javascript - Angular 2/4 |类型 '{}' 不可分配给类型 'Object[]'

不同环境下的Cordova插件管理

javascript - ionic 后退按钮对齐到标题的右侧

javascript - "echarts"' has no exported member named ' EChartOption '. Did you mean ' EChartsOption' 代替?

Javascript 作用域

javascript - 如何获取最接近的id?

javascript - 错误类型错误 : Cannot read property 'clientId' of null

javascript - 正则表达式允许一个逗号而不是在 JavaScript 中字符串的开头或结尾

angular - 如何使用 MatPaginatorIntl?

android - 更新cordova-android 6.3.0-> 6.4.0 =>未找到Gradle