javascript - JavaScript 中的 indexOf 方法

标签 javascript if-statement indexof

我试图理解 Javascript 的许多基本组件,我遇到的其中一件事是一行代码说

if (varX.indexOf(String(varY),0) < 0)    

varX 是一个字符串数组,而 varY 显然是该数组中的一个字符串。去掉“,0”,我明白代码只是在寻找带有数组 varX 的 varY。但我不知道 ,0 的作用以及 if 语句的含义。我尽我所能来查找它,但没有真正发现任何东西。

最佳答案

根据MDN docs :

fromindex

The index to start the search at. If the index is greater than or equal to the array's length, -1 is returned, which means the array will not be searched. If the provided index value is a negative number, it is taken as the offset from the end of the array. Note: if the provided index is negative, the array is still searched from front to back. If the calculated index is less than 0, then the whole array will be searched. Default: 0 (Entire array is searched).

因此,传入“0”几乎毫无意义,因为它无论如何都会从 0 开始搜索。

关于javascript - JavaScript 中的 indexOf 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27491600/

相关文章:

javascript - javascript 中是否有 'OrElse' 等效项?

javascript - React-Native (FlatList) : How to know when the rendering of visible items has finished

python - 使用 julia 的数组或条件评估集

javascript - 如何测试一个字符串是否是另一个字符串的最后一个 “part”?

计算字符串中辅音的个数

javascript - IndexedDB:查询索引值 "contains"

javascript - 从 javascript 函数返回一个值,然后执行其中的代码

java - struts 2 if标签用于比较字符类型

if-statement - CoffeeScript 中的 switch case 语句

backbone.js - 集合中的下划线indexOf