首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >foreach中的if /else条件

问foreach中的if /else条件
EN

Stack Overflow用户
提问于 2019-03-07 21:00:46
回答 3查看 1.5K关注 0票数 0

我正在开发一个angular项目。我有一个表文件,我想将每个元素与一个数据值进行比较,如果它是正确的,我会做语句,否则我会做另一个通信,但我的问题是,即使数据是正确的,它总是获取所有的表,并且应该在很短的时间内在else中访问。请告诉我如何避免这种情况。

下面是我的代码:

代码语言:javascript
复制
if (this.data) {
  this.imgNotFoundText = '';
  this.data.package.files.forEach(element => {
    i++;
    this.picture = '';
    if (element.name == this.data.properties.Name) {
      this.picture = 'picOne.png'
    }
    if (i == this.data.package.files.length && this.picture == '') {
      this.picture = './../assets/img/notFound.jpg'
    }
  });
}
EN

回答 3

Stack Overflow用户

发布于 2019-03-07 21:06:49

我看到了几个可能的问题:

  1. 似乎总是在循环中无条件地做this.picture = '';是可疑的。如果要这样做,不妨只查看数组中的最后一项。您可能希望将其移到forEach调用之前。
  2. 您引用了一个else,但您的代码中没有else。您有两个连续的if,但是第一个if的结果对第二个没有任何影响。您可能想要else if。然后,如果第一个if中的条件为真,则不执行第二个if。

因此,如果这两个猜测都是正确的:

代码语言:javascript
复制
if (this.data) {
    this.imgNotFoundText = '';
    this.picture = '';
    this.data.package.files.forEach(element => {
        i++;
        if (element.name == this.data.properties.Name) {
            this.picture = 'picOne.png'
        } else if (i == this.data.package.files.length && this.picture == '') {
            this.picture = './../assets/img/notFound.jpg'
        }
    });
}

附注:您还没有展示i是如何初始化的,但是如果使用它来跟踪forEach当前条目的索引,就没有必要了:forEach将其作为第二个参数接收:

代码语言:javascript
复制
if (this.data) {
    this.imgNotFoundText = '';
    this.picture = '';
    this.data.package.files.forEach((element, index) => {
    // -----------------------------^^^^^^^^^^^^^^^^
        if (element.name == this.data.properties.Name) {
            this.picture = 'picOne.png'
        } else if (index == this.data.package.files.length && this.picture == '') {
    // ------------^^^^^
            this.picture = './../assets/img/notFound.jpg'
        }
    });
}

您可能还希望完全避免第二个if,只需在循环之前指定"not found“默认值:

代码语言:javascript
复制
if (this.data) {
    this.imgNotFoundText = '';
    const {files} = this.data.package;
    this.picture = files.length ? './../assets/img/notFound.jpg' : '';
    files.forEach(element => {
        if (element.name == this.data.properties.Name) {
            this.picture = 'picOne.png'
        }
    });
}

在这种情况下,我假设如果files中没有条目,this.picture应该为'',或者如果至少有一个条目,则为"not found“图像。如果找到匹配项,循环将覆盖它。

接下来,除非files中可能有多个条目具有相同的name,否则您可能希望在第一个匹配时停止。所以:

代码语言:javascript
复制
if (this.data) {
    this.imgNotFoundText = '';
    const {files} = this.data.package;
    this.picture = files.length ? './../assets/img/notFound.jpg' : '';
    for (const {name} of files) {
        if (name == this.data.properties.Name) {
            this.picture = 'picOne.png'
            break;
        }
    }
}
票数 5
EN

Stack Overflow用户

发布于 2019-03-07 21:07:53

不确定您的目标是什么,但您只是迭代列表并每次设置图片值,这意味着您将得到最后一个元素的图片值。

如果您的目标是为没有文件的元素显示“未找到”图片,则需要有一个反映文件数组的图片数组(或为每个文件添加一个属性图片)。

票数 0
EN

Stack Overflow用户

发布于 2019-03-07 21:19:57

看起来你可以使用Array.prototype.some了

代码语言:javascript
复制
if (this.data) {
  this.imgNotFoundText = '';
  this.picture = this.data.package.files.some(
  (element) => element.name === this.data.properties.Name
  ) ? 'picOne.png' : './../assets/img/notFound.jpg'
}

如果data.package.files中的任何项的名称与this.data.properties.Name相同,它将返回picOne.png,否则将返回notFound.jpg

尽管这可能会

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55044413

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档