简体   繁体   English

tslint / codelyzer / ng lint 错误:“for (... in ...) 语句必须用 if 语句过滤”

[英]tslint / codelyzer / ng lint error: “for (… in …) statements must be filtered with an if statement”

Lint error message:棉绒错误消息:

src/app/detail/edit/edit.component.ts[111, 5]: for (... in ...) statements must be filtered with an if statement src/app/detail/edit/edit.component.ts[111, 5]: for (... in ...) 语句必须用 if 语句过滤

Code snippet (It is a working code. It is also available atangular.io form validation section ):代码片段(这是一个工作代码。它也可以在angular.io 表单验证部分找到):

for (const field in this.formErrors) {
      // clear previous error message (if any)
      this.formErrors[field] = '';
      const control = form.get(field);

      if (control && control.dirty && !control.valid) {
        const messages = this.validationMessages[field];
        for (const key in control.errors) {
          this.formErrors[field] += messages[key] + ' ';
        }
      }
    }

Any idea how to fix this lint error?知道如何修复这个 lint 错误吗?

To explain the actual problem that tslint is pointing out, a quote from the JavaScript documentation of the for...in statement :为了解释tslint 指出的实际问题,引用来自for...in 语句的 JavaScript 文档:

The loop will iterate over all enumerable properties of the object itself and those the object inherits from its constructor's prototype (properties closer to the object in the prototype chain override prototypes' properties).循环将迭代对象本身的所有可枚举属性以及对象从其构造函数的原型继承的属性(更接近原型链中对象的属性覆盖原型的属性)。

So, basically this means you'll get properties you might not expect to get (from the object's prototype chain).所以,基本上这意味着您将获得您可能不期望获得的属性(从对象的原型链)。

To solve this we need to iterate only over the objects own properties.为了解决这个问题,我们只需要迭代对象自己的属性。 We can do this in two different ways (as suggested by @Maxxx and @Qwertiy).我们可以通过两种不同的方式来做到这一点(如@Maxxx 和@Qwertiy 所建议的那样)。

First solution第一个解决方案

for (const field of Object.keys(this.formErrors)) {
    ...
}

Here we utilize the Object.Keys() method which returns an array of a given object's own enumerable properties, in the same order as that provided by a for...in loop (the difference being that a for-in loop enumerates properties in the prototype chain as well).这里我们使用Object.Keys()方法,该方法返回给定对象自己的可枚举属性的数组,其顺序与 for...in 循环提供的顺序相同(区别在于 for-in 循环在原型链也是如此)。

Second solution第二种解决方案

for (var field in this.formErrors) {
    if (this.formErrors.hasOwnProperty(field)) {
        ...
    }
}

In this solution we iterate all of the object's properties including those in it's prototype chain but use the Object.prototype.hasOwnProperty() method, which returns a boolean indicating whether the object has the specified property as own (not inherited) property, to filter the inherited properties out.在此解决方案中,我们迭代对象的所有属性,包括其原型链中的属性,但使用Object.prototype.hasOwnProperty()方法,该方法返回一个布尔值,指示对象是否具有指定的属性作为自己的(非继承的)属性,以过滤继承的属性出来了。

应用@Helzgate 回复的一种更简洁的方法可能是将您的 'for .. in' 替换为

for (const field of Object.keys(this.formErrors)) {
for (const field in this.formErrors) {
  if (this.formErrors.hasOwnProperty(field)) {
for (const key in control.errors) {
  if (control.errors.hasOwnProperty(key)) {

use Object.keys:使用 Object.keys:

Object.keys(this.formErrors).map(key => {
  this.formErrors[key] = '';
  const control = form.get(key);

  if(control && control.dirty && !control.valid) {
    const messages = this.validationMessages[key];
    Object.keys(control.errors).map(key2 => {
      this.formErrors[key] += messages[key2] + ' ';
    });
  }
});

If the behavior of for(... in ...) is acceptable/necessary for your purposes, you can tell tslint to allow it.如果 for(... in ...) 的行为对于您的目的是可以接受/必要的,您可以告诉 tslint 允许它。

in tslint.json, add this to the "rules" section.在 tslint.json 中,将其添加到“规则”部分。

"forin": false

Otherwise, @Maxxx has the right idea with否则,@Maxxx 有正确的想法

for (const field of Object.keys(this.formErrors)) {

I think this message is not about avoiding to use switch .我认为这条消息不是关于避免使用switch Instead it wants you to check for hasOwnProperty .相反,它希望您检查hasOwnProperty The background can be read here: https://stackoverflow.com/a/16735184/1374488背景可以在这里阅读: https : //stackoverflow.com/a/16735184/1374488

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM