💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
返回某个值在数组中的第一个匹配项的索引。 ## 语法 ~~~ array1.indexOf(searchElement[, fromIndex]) ~~~ ## 参数 |参数|定义| |--|--| |array1|必需。一个数组对象。| |searchElement|必需。要在 array1 中定位的值。| |fromIndex|可选。用于开始搜索的数组索引。如果省略 fromIndex,则从索引 0 处开始搜索。| ## 返回值 数组中的 searchElement 的第一个匹配项的索引;如果未找到 searchElement,则为 -1。 ## 备注 indexOf 方法在数组中搜索指定的值。该方法返回第一个匹配项的索引;如果找不到指定的值,则为 -1。 按升序索引顺序执行搜索。 数组元素将与 searchElement 值进行全等比较,与 === 运算符类似。 可选 fromIndex 参数指定用于开始搜索的数组索引。如果 fromIndex 大于或等于数组长度,则返回 -1。如果 fromIndex 为负,则搜索从数组长度加上 fromIndex 的位置处开始。 下面的示例阐释了 indexOf 方法的用法。 ~~~ // Create an array. (The elements start at index 0.) var ar = ["ab", "cd", "ef", "ab", "cd"]; // Determine the first location of "cd". document.write(ar.indexOf("cd") + "<br/>"); // Output: 1 // Find "cd" starting at index 2. document.write(ar.indexOf("cd", 2) + "<br/>"); // Output: 4 // Find "gh" (which is not found). document.write (ar.indexOf("gh")+ "<br/>"); // Output: -1 // Find "ab" with a fromIndex argument of -2. // The search starts at index 3, which is the array length plus -2. document.write (ar.indexOf("ab", -2) + "<br/>"); // Output: 3 ~~~