此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。
Array.prototype.indexOf()
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2015年7月.
indexOf() 方法返回数组中第一次出现给定元素的下标,如果不存在则返回 -1。
In this article
尝试一下
const beasts = ["ant", "bison", "camel", "duck", "bison"];console.log(beasts.indexOf("bison"));// Expected output: 1// Start from index 2console.log(beasts.indexOf("bison", 2));// Expected output: 4console.log(beasts.indexOf("giraffe"));// Expected output: -1语法
js
indexOf(searchElement)indexOf(searchElement, fromIndex)参数
searchElement数组中要查找的元素。
fromIndex可选开始搜索的索引(从零开始),会转换为整数。
- 负索引从数组末尾开始计数——如果
frommindex < 0,使用frommindex + array.length。注意,在这种情况下,仍然从前到后搜索数组。 - 如果
fromIndex < -array.length或者省略了fromIndex,将使用0,而导致整个数组被搜索。 - 如果
fromIndex >= array.length,数组不会继续搜索并返回-1。
- 负索引从数组末尾开始计数——如果
返回值
首个被找到的元素在数组中的索引位置; 若没有找到则返回-1。
描述
indexOf() 使用严格相等(与=== 运算符使用的算法相同)将searchElement 与数组中的元素进行比较。NaN 值永远不会被比较为相等,因此当searchElement 为NaN 时indexOf() 总是返回-1。
indexOf() 方法会跳过稀疏数组中的空槽。
indexOf() 方法是通用的。它只期望this 值具有length 属性和整数键属性。
示例
>使用 indexOf()
以下例子使用indexOf() 方法确定多个值在数组中的位置。
js
const array = [2, 9, 9];array.indexOf(2); // 0array.indexOf(7); // -1array.indexOf(9, 2); // 2array.indexOf(2, -1); // -1array.indexOf(2, -3); // 0你没法使用indexOf() 来搜索NaN。
js
const array = [NaN];array.indexOf(NaN); // -1找出指定元素出现的所有位置
js
const indices = [];const array = ["a", "b", "a", "c", "a", "d"];const element = "a";let idx = array.indexOf(element);while (idx !== -1) { indices.push(idx); idx = array.indexOf(element, idx + 1);}console.log(indices);// [0, 2, 4]判断一个元素是否在数组里,不在则更新数组
js
function updateVegetablesCollection(veggies, veggie) { if (veggies.indexOf(veggie) === -1) { veggies.push(veggie); console.log(`New veggies collection is: ${veggies}`); } else { console.log(`${veggie} already exists in the veggies collection.`); }}const veggies = ["potato", "tomato", "chillies", "green-pepper"];updateVegetablesCollection(veggies, "spinach");// New veggies collection is: potato,tomato,chillies,green-pepper,spinachupdateVegetablesCollection(veggies, "spinach");// spinach already exists in the veggies collection.在稀疏数组中使用 indexOf()
不能使用indexOf() 在稀疏数组中搜索空槽。
js
console.log([1, , 3].indexOf(undefined)); // -1在非数组对象上调用 indexOf()
indexOf() 方法读取this 的length 属性,然后访问每个整数索引。
js
const arrayLike = { length: 3, 0: 2, 1: 3, 2: 4,};console.log(Array.prototype.indexOf.call(arrayLike, 2));// 0console.log(Array.prototype.indexOf.call(arrayLike, 5));// -1规范
| Specification |
|---|
| ECMAScript® 2026 Language Specification> # sec-array.prototype.indexof> |