JavaScript .findIndex方法详解

JavaScript .findIndex方法详解

JavaScript .findIndex方法详解

在JavaScript中,.findIndex()方法是用于查找数组中满足指定条件的元素的索引的方法。该方法会返回第一个满足条件的元素的索引,如果没有找到满足条件的元素,则返回-1。本文将详细介绍.findIndex()方法的用法及示例代码。

语法

.findIndex()方法的语法如下:

array.findIndex(function(currentValue, index, arr), thisValue)
  • function(currentValue, index, arr): 用于测试每个元素的函数,函数参数包括当前元素的值(currentValue)、当前元素的索引(index)以及数组本身(arr)。
  • thisValue (可选): 传递给函数的值,用作函数内部的this值。

示例代码

示例1:查找数组中大于10的元素的索引

const numbers = [5, 12, 8, 15, 3];
const index = numbers.findIndex(num => num > 10);

console.log(index); // 输出: 1

Output:

JavaScript .findIndex方法详解

示例2:查找数组中包含指定字符串的元素的索引

const fruits = ['apple', 'banana', 'orange', 'grape'];
const index = fruits.findIndex(fruit => fruit.includes('an'));

console.log(index); // 输出: 1

Output:

JavaScript .findIndex方法详解

示例3:查找数组中长度大于5的字符串的索引

const words = ['hello', 'world', 'geek-docs', 'javascript'];
const index = words.findIndex(word => word.length > 5);

console.log(index); // 输出: 2

Output:

JavaScript .findIndex方法详解

示例4:查找数组中偶数元素的索引

const nums = [1, 2, 3, 4, 5, 6];
const index = nums.findIndex(num => num % 2 === 0);

console.log(index); // 输出: 1

Output:

JavaScript .findIndex方法详解

示例5:查找数组中第一个负数的索引

const values = [10, -5, 8, -3, 12];
const index = values.findIndex(value => value < 0);

console.log(index); // 输出: 1

Output:

JavaScript .findIndex方法详解

示例6:查找数组中第一个大于100的元素的索引

const data = [50, 120, 80, 150, 30];
const index = data.findIndex(num => num > 100);

console.log(index); // 输出: 1

Output:

JavaScript .findIndex方法详解

示例7:查找数组中第一个以’h’开头的字符串的索引

const names = ['alice', 'bob', 'harry', 'dave'];
const index = names.findIndex(name => name.startsWith('h'));

console.log(index); // 输出: 2

Output:

JavaScript .findIndex方法详解

示例8:查找数组中第一个包含数字的字符串的索引

const items = ['apple', '123', 'orange', '456'];
const index = items.findIndex(item => /\d/.test(item));

console.log(index); // 输出: 1

Output:

JavaScript .findIndex方法详解

示例9:查找数组中第一个长度为3的字符串的索引

const words = ['cat', 'dog', 'elephant', 'lion'];
const index = words.findIndex(word => word.length === 3);

console.log(index); // 输出: 0

Output:

JavaScript .findIndex方法详解

示例10:查找数组中第一个元素为假值的索引

const values = [10, 0, '', false, true];
const index = values.findIndex(value => !value);

console.log(index); // 输出: 1

Output:

JavaScript .findIndex方法详解

示例11:查找数组中第一个元素为真值的索引

const flags = [false, 0, '', true, 'geek-docs'];
const index = flags.findIndex(flag => flag);

console.log(index); // 输出: 3

Output:

JavaScript .findIndex方法详解

示例12:查找数组中第一个元素为null的索引

const data = [10, null, 20, null, 30];
const index = data.findIndex(value => value === null);

console.log(index); // 输出: 1

Output:

JavaScript .findIndex方法详解

示例13:查找数组中第一个元素为undefined的索引

const values = [10, undefined, 20, null, 30];
const index = values.findIndex(value => value === undefined);

console.log(index); // 输出: 1

Output:

JavaScript .findIndex方法详解

示例14:查找数组中第一个元素为NaN的索引

const numbers = [10, NaN, 20, 30, NaN];
const index = numbers.findIndex(num => isNaN(num));

console.log(index); // 输出: 1

Output:

JavaScript .findIndex方法详解

示例15:查找数组中第一个元素为对象的索引

const items = [10, {name: 'apple'}, 20, {name: 'orange'}];
const index = items.findIndex(item => typeof item === 'object');

console.log(index); // 输出: 1

Output:

JavaScript .findIndex方法详解

示例16:查找数组中第一个元素为数组的索引

const data = [10, [1, 2, 3], 20, [4, 5, 6]];
const index = data.findIndex(item => Array.isArray(item));

console.log(index); // 输出: 1

Output:

JavaScript .findIndex方法详解

示例17:查找数组中第一个元素为函数的索引

const values = [10, () => {}, 20, () => {}];
const index = values.findIndex(value => typeof value === 'function');

console.log(index); // 输出: 1

Output:

JavaScript .findIndex方法详解

示例18:查找数组中第一个元素为正则表达式的索引

const patterns = [/\d+/, 'geek-docs', /[a-z]+/];
const index = patterns.findIndex(pattern => pattern instanceof RegExp);

console.log(index); // 输出: 0

Output:

JavaScript .findIndex方法详解

示例19:查找数组中第一个元素为日期对象的索引

const dates = [new Date(), '2022-01-01', new Date('2023-01-01')];
const index = dates.findIndex(date => date instanceof Date);

console.log(index); // 输出: 0

Output:

JavaScript .findIndex方法详解

示例20:查找数组中第一个元素为Map对象的索引

const items = [new Map(), {name: 'apple'}, new Map()];
const index = items.findIndex(item => item instanceof Map);

console.log(index); // 输出: 0

Output:

JavaScript .findIndex方法详解

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程