简体   繁体   English

如何使用 forEach 检查数组中是否存在匹配项 (JavaScript)

[英]How to use forEach to check if there is a match in the array (JavaScript)

How to use forEach to check if there is a match in the array (JavaScript)如何使用 forEach 检查数组中是否存在匹配项 (JavaScript)

I'm not sure exactly what you're asking;我不确定你到底在问什么; how do you "log out" variables in code?你如何在代码中“注销”变量?

In general, you don't need a loop to check existence an array;通常,您不需要循环来检查数组是否存在; that's what the Array.includes method is for:这就是Array.includes方法的用途:

if (files.includes(upload.type)) {
    console.log("type is OK")
} else {
    console.log(`type ${upload.type} not found`)
}

const isMatch = files.some((type) => upload.type === type); const isMatch = files.some((type) => upload.type === type);

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

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