在javascript中查找数组元素索引最常用的方法是indexof(),它返回指定元素首次出现的索引,若未找到则返回-1;2. indexof()使用严格相等(===)比较,因此类型和值都必须匹配;3. 该方法可接受第二个参数fromindex,用于指定查找起始位置,若该值大于等于数组长度则直接返回-1;4. indexof()无法查找nan,因为nan不等于自身,此时应使用findindex()结合isnan()或includes()方法;5. 对于对象数组或复杂条件查找,应使用findindex(),因为它接受回调函数并可自定义匹配逻辑;6. 要查找某元素所有匹配的索引,需通过循环结合indexof()的fromindex参数反复查找,每次从上一个匹配位置的后一位开始;7. indexof()能正常查找undefined和null,但在处理特殊值和复杂数据结构时需注意其局限性。

在 JavaScript 里,要查找数组中元素的索引,最直接也最常用的方法就是使用数组自带的
indexOf()
-1
indexOf()
它的基本语法是这样的:
arr.indexOf(searchElement[, fromIndex])
searchElement
fromIndex
indexOf
-1
让我们看几个例子,这会让你对它的行为模式有个更清晰的认识:
const fruits = ['apple', 'banana', 'orange', 'apple', 'grape'];
// 查找 'banana' 的索引
const bananaIndex = fruits.indexOf('banana');
console.log(bananaIndex); // 输出: 1
// 查找 'apple' 的索引,注意它只返回第一个匹配的
const firstAppleIndex = fruits.indexOf('apple');
console.log(firstAppleIndex); // 输出: 0
// 查找一个不存在的元素
const kiwiIndex = fruits.indexOf('kiwi');
console.log(kiwiIndex); // 输出: -1
// 从特定索引开始查找
// 查找从索引 2('orange')开始的 'apple'
const secondAppleIndex = fruits.indexOf('apple', 2);
console.log(secondAppleIndex); // 输出: 3
// 数组中没有的元素,即使指定了起始索引也一样
const noSuchElement = fruits.indexOf('mango', 0);
console.log(noSuchElement); // 输出: -1
// fromIndex 大于等于数组长度,直接返回 -1
const fromTooFar = fruits.indexOf('apple', 10);
console.log(fromTooFar); // 输出: -1indexOf()
===
5
'5'
indexOf()
const mixedArray = [1, '2', 3, '4'];
console.log(mixedArray.indexOf(1)); // 输出: 0
console.log(mixedArray.indexOf('2')); // 输出: 1
console.log(mixedArray.indexOf(2)); // 输出: -1 (数字 2 不存在,只有字符串 '2')这是个经常被问到的问题,因为它们听起来功能很相似,但实际应用场景却大相径庭。简单来说,
indexOf
findIndex
indexOf()
===
searchElement
null
undefined
const users = [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }];
const targetUser = { id: 1, name: 'Alice' };
// 这会返回 -1,因为 targetUser 和数组中的第一个对象虽然内容一样,但不是同一个对象引用
console.log(users.indexOf(targetUser)); // 输出: -1findIndex()
true
findIndex
true
-1
findIndex
const users = [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }];
// 查找 id 为 2 的用户的索引
const bobIndex = users.findIndex(user => user.id === 2);
console.log(bobIndex); // 输出: 1
// 查找 name 包含 'ice' 的用户的索引
const aliceIndex = users.findIndex(user => user.name.includes('ice'));
console.log(aliceIndex); // 输出: 0
// 查找一个不存在的条件
const charlieIndex = users.findIndex(user => user.name === 'Charlie');
console.log(charlieIndex); // 输出: -1所以,当你只需要找到一个精确匹配的原始值时,用
indexOf
findIndex
indexOf()
indexOf()
fromIndex
思路是这样的:先找到第一个,然后从第一个的下一个位置开始继续找,直到找不到为止。
下面是一个实现这个逻辑的例子:
function findAllIndices(arr, searchElement) {
const indices = [];
let currentIndex = arr.indexOf(searchElement); // 首次查找
while (currentIndex !== -1) {
indices.push(currentIndex); // 找到了,记录下来
// 从当前找到的索引的下一个位置开始继续查找
currentIndex = arr.indexOf(searchElement, currentIndex + 1);
}
return indices;
}
const numbers = [1, 2, 3, 2, 4, 2, 5];
const allTwoIndices = findAllIndices(numbers, 2);
console.log(allTwoIndices); // 输出: [1, 3, 5]
const allOneIndices = findAllIndices(numbers, 1);
console.log(allOneIndices); // 输出: [0]
const allSixIndices = findAllIndices(numbers, 6);
console.log(allSixIndices); // 输出: []这个
while
indexOf
在 JavaScript 中,一些特殊的值在比较行为上会有一些“怪异”的地方,这也会影响到
indexOf()
NaN
indexOf()
NaN
NaN
NaN === NaN
false
indexOf
NaN
const weirdNumbers = [1, 2, NaN, 3]; console.log(weirdNumbers.indexOf(NaN)); // 输出: -1
如果你需要查找数组中是否存在
NaN
NaN
indexOf()
findIndex()
isNaN()
console.log(weirdNumbers.findIndex(item => isNaN(item))); // 输出: 2
或者,如果你只是想知道数组中是否包含
NaN
Array.prototype.includes()
NaN
console.log(weirdNumbers.includes(NaN)); // 输出: true
undefined
indexOf()
undefined
const mixedValues = [1, undefined, 3, null]; console.log(mixedValues.indexOf(undefined)); // 输出: 1
null
indexOf()
null
const mixedValues = [1, undefined, 3, null]; console.log(mixedValues.indexOf(null)); // 输出: 3
总之,
indexOf()
NaN
findIndex()
以上就是js 如何用indexOf查找数组中元素的索引的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号