使用 .includes() 的 Javascript 'if' 语句

r_c*_*ill 2 javascript arrays

我有一个包含文件路径作为字符串的数组。我需要搜索这个数组并创建一个新数组,只包含那些包含某个单词的数组。我将如何创建包含“includes”方法的 if 语句?

到目前为止,这是我的代码:

var imagePaths = [...]

if(imagePaths.includes('index') === 'true'){
 ???
}
Run Code Online (Sandbox Code Playgroud)

先感谢您

T.J*_*der 10

您不需要将布尔值与任何东西进行比较;只需使用它们:

if (imagePaths.includes('index')) {
    // Yes, it's there
}
Run Code Online (Sandbox Code Playgroud)

或者

if (!imagePaths.includes('index')) {
    // No, it's not there
}
Run Code Online (Sandbox Code Playgroud)

如果您确实决定将布尔值与某物进行比较(这通常是不好的做法),请与trueor false、 not 'true'(这是一个字符串)进行比较。