[英]if /else condition inside foreach
I am developing an angular project. 我正在开发一个有角度的项目。 I have a table file I want to compare each element with a data value if it is correct I will do statement else I will do another comportement but my problem is that even the data is correct it always fetches all the table and should access in the else for a very short time.
我有一个表文件,如果要正确,我想将每个元素与一个数据值进行比较,我会做声明,否则我会做另一次比较,但是我的问题是,即使数据是正确的,它也总是会获取所有表,并且应该在其他很短的时间。 How to avoid that, please.
请如何避免这种情况。
Here is my code: 这是我的代码:
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'
}
});
}
I see a couple of possible problems: 我看到几个可能的问题:
It seems suspect to always do this.picture = '';
似乎总是这样做
this.picture = '';
unconditionally in the loop. 无条件循环。 If you're going to do that, you may as well only look at the last entry in the array.
如果要执行此操作,则最好只查看数组中的最后一个条目。 You probably want to move that to before the
forEach
call. 您可能希望将其移动到
forEach
调用之前 。
You've referred to an else
, but there is no else
in your code. 您已经提到了
else
,但是代码中没有else
。 You have two if
s in a row, but the result of the first if
doesn't have any effect at all on the second one. 您连续有两个
if
,但是第一个if
的结果对第二个if
根本没有任何影响。 You may have wanted else if
. 如果您可能想要
else if
。 Then, the second if
isn't performed if the condition in the first if
was true. 然后,
if
第一个if
的条件为true,则不执行第二个if
。
So if both of those guesses are right: 因此,如果这些猜测都正确:
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'
}
});
}
Side note: You haven't shown how i
is initialized, but if it's used to track the index of the current entry of the forEach
, there's no need: forEach
receives that as a second argument: 旁注:您尚未显示如何初始化
i
,但是如果将其用于跟踪forEach
当前条目的索引,则不需要: forEach
其作为第二个参数接收:
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'
}
});
}
You also might want to avoid that second if
altogether, by just specifying the "not found" default before the loop: 您还可能希望避免第二
if
完全由刚刚指定的循环之前的“未找到”默认:
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'
}
});
}
In that I've assumed this.picture
should be ''
if there are no entries in files
, or the "not found" image if there's at least one entry. 在这种情况下,我假设如果
files
中没有任何条目,则this.picture
应该为''
如果至少有一个条目,则为“未找到”图像。 The loop will overwrite it if it finds a match. 如果找到匹配项,循环将覆盖它。
Continuing from there, unless there can be multiple entries in files
with the same name
, you probably want to stop as of the first match. 从那里继续,除非
files
有多个具有相同name
条目,否则您可能要从第一个匹配项开始停止。 So: 所以:
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;
}
}
}
Not sure what your goal is but you're just iterating through the list and setting the picture value every time, this means you'll have as result the last element's picture value. 不确定您的目标是什么,但是您只是遍历列表并每次设置图片值,这意味着您将获得最后一个元素的图片值。
If your goal is to display a "not found" picture for elements who don't have files, you need to have an array of pictures that reflects the array of files (or add a property picture to each file). 如果您的目标是为没有文件的元素显示“未找到”图片,则需要有一个图片数组来反映文件数组(或向每个文件添加属性图片)。
Looks like you can use Array.prototype.some 看起来您可以使用Array.prototype.some
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'
}
It'll return picOne.png
if any of the items in data.package.files has a name that is same as this.data.properties.Name or it'll return notFound.jpg
如果
picOne.png
任何项目具有与this.data.properties.Name相同的名称,则它将返回picOne.png
,否则它将返回notFound.jpg
Although that could 虽然那可以
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.