【问题标题】:AngularJS foreach loop with JsHint and eq operator带有 JsHint 和 eq 运算符的 AngularJS foreach 循环
【发布时间】:2014-09-04 05:20:09
【问题描述】:

我在 Grunt 的 JSHint 上有一个错误,它大致如下:

line 48  col 23  Expected '===' and instead saw '=='.

使用 '===' 会破坏代码。

我在 Google 附近,显然禁用此选项的选项已损坏(使用截至 2014 年 9 月 4 日的最新版本)

我的代码是这样的:

$scope.getStationById = function(stationId)
{
    var parsedId = stationId,
    foundStation;

    angular.forEach($scope.stations, function(station) 
    {
        if(station.id == parsedId)
        {
            foundStation = station;
        }
    });

    return foundStation;
};

关于如何消除这个烦人的错误有什么建议吗?

编辑

控制台日志:

console.log(station.id + " | " + parsedId);

生产:

1 | 1
3 | 1
5 | 1

【问题讨论】:

  • 检查两个操作数的类型。它们不能相同。
  • 添加以下日志语句并告诉输出是什么,console.log(typeof station.id);控制台.log(类型 parseId)。我认为一个是字符串,另一个是数字

标签: javascript angularjs gruntjs jshint


【解决方案1】:

我假设您尝试禁用它的方式是在选项中使用eqeqeq: false

你可以尝试添加一个替代方法

// jshint ignore:line

在行尾

/* jshint ignore:start */
...
/* jshint ignore:end */

围绕一个代码块,根据docs

最好的方法可能仍然是修复代码,这样它就不必依赖类型强制来工作。您可以为此使用parseInt(str, 10)

【讨论】:

  • 第二段代码虽然有效,但忽略多行块。
猜你喜欢
  • 2019-08-06
  • 2014-01-20
  • 2021-10-19
  • 1970-01-01
  • 1970-01-01
  • 2019-06-05
  • 2016-06-03
  • 2015-07-09
  • 2017-03-13
相关资源
最近更新 更多