Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

js findindex方法

作者:醉眼看苍生   发布日期:2025-06-10   浏览:26

// 使用 findIndex 方法查找数组中第一个符合条件的元素索引

const array = [5, 12, 8, 130, 44];

// 查找第一个大于 10 的元素的索引
const index = array.findIndex(element => element > 10);

console.log(index); // 输出: 1 (因为 12 是第一个大于 10 的元素,其索引为 1)

// 如果没有找到符合条件的元素,则返回 -1
const notFoundIndex = array.findIndex(element => element > 200);

console.log(notFoundIndex); // 输出: -1 (因为没有元素大于 200)

解释说明:

  • findIndex 方法会遍历数组,并对每个元素执行提供的回调函数。
  • 回调函数接收三个参数:当前元素、当前索引和整个数组(通常只需要使用当前元素)。
  • 一旦找到符合条件的元素,findIndex 会立即返回该元素的索引。
  • 如果没有任何元素符合条件,则返回 -1

上一篇:js string 转number

下一篇:js findindex

大家都在看

js 数组对象排序

js 数组删掉第一个值

js fill

js 数组复制

js 复制数组

js 数组拷贝

js 对象转数组

js 深拷贝数组

js 获取今天年月日

js jsonp

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站