为什么不执行 if 语句

时间:2021-01-25 08:02:15

标签: javascript

有一个任务是统计单词“not_available”的个数,如果是12则发送1,如果至少有一个是“available”则发送0

下面是一个示例代码,即使“not_available”的数量= 12,它仍然发送0

我做错了什么?

var Count_available = (rootObj.match(/AVAILABLE/g) || []).length
var Count_unavailable = (rootObj.match(/NOT_AVAILABLE/g) || []).length

if (Count_available >= 1) {
  var jsondata = {
    "type": "SberLogistic.Availability",
    "series": [{
      "timeseriesId": "custom:syntheticmonitors.availability",
      "dimensions": {
        "config_id": "CI02874375"
      },
      "dataPoints": [
        [Number(new Date()), 1]
      ] //send 1

    }]
  }
} else if (Count_unavailable == 12) {
  var jsondata = {
    "type": "SberLogistic.Availability",
    "series": [{
      "timeseriesId": "custom:syntheticmonitors.availability",
      "dimensions": {
        "config_id": "CI02874375"
      },
      "dataPoints": [
        [Number(new Date()), 0]
      ] //send 0

    }]
  }
};

1 个答案:

答案 0 :(得分:3)

如果至少有 1 个可用,它将返回而不检查 else if。

颠倒顺序,然后在可用之前检查不可用。

    if (Count_unavailable == 12) {
    ...
    } else if (Count_available >= 1) {
    ...
    };