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

js findindex

作者:残缺的不完美   发布日期:2025-05-20   浏览:57

// 示例代码:使用 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 方法会遍历数组,并对每个元素执行提供的回调函数。
  • 回调函数接收三个参数:当前元素、当前索引和整个数组(通常只用到当前元素)。
  • 如果回调函数返回 true,则 findIndex 返回该元素的索引;如果遍历完所有元素都没有找到符合条件的元素,则返回 -1

上一篇:js findindex方法

下一篇:js tofixed

大家都在看

js 数组对象排序

js 数组删掉第一个值

js fill

js 数组复制

js 复制数组

js 数组拷贝

js 对象转数组

js 深拷贝数组

js 获取今天年月日

js jsonp

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

Laravel 中文站