无法使用字符串检查相等的数字

Cannot check for an equal number using string

本文关键字:数字 检查 字符串      更新时间:2023-09-26

我有一个我认为简单的逻辑检查。在我的代码中

$scope.seatMap.PlaneTypeCode = "175"

然而,当我设置时

$scope.seatMap.PlaneTypeCode === "175" //my debugger returns <b>false </b>
parseInt($scope.seatMap.PlaneTypeCode,10) ===175  // equals 17

我在基数上加了几个零,但无济于事。

我不知道如何进行比较检查。对此有任何见解,我们将不胜感激。

这是我完整的if语句

if (parseInt(col.name,10) ===4 && parseInt($scope.seatMap.PlaneTypeCode,10) ===175 && $scope.TripSummary) {
       col.available = false;
    }

******更改了对此的响应

if (parseInt(col.name,10) ===4 && $scope.seatMap.PlaneTypeCode ==="175" && $scope.TripSummary) {
            col.available = false;
        }  // still getting false

===是最佳实践,您应该使用它。查看@Joyson 提供的参考

您不需要parseInt中的,10,因为它是默认值。

var PlaneTypeCode = "175";
if (parseInt(PlaneTypeCode) === 175) {
  console.log('equal');
}

如果PlaneTypeCode是一个代码,并且可以包含数字以外的任何内容,则更好的比较是:

if (PlaneTypeCode === "175")

您可以使用==而不是===

$scope.seatMap.PlaneTypeCode == "175"

请参阅===和===之间的差异以了解更多

使用angular.equals($scope.seatMap.PlaneTypeCode,"175")