// 使用 Array.prototype.findIndex 方法查找数组中满足条件的元素索引
const array = [1, 3, 5, 8, 10];
// 查找第一个大于 5 的元素的索引
const index = array.findIndex(element => element > 5);
console.log(index); // 输出: 3
// 解释说明:
// findIndex 方法会遍历数组中的每个元素,并执行提供的回调函数。
// 回调函数接收三个参数:当前元素、当前索引和整个数组。
// 当回调函数返回 true 时,findIndex 返回当前元素的索引。
// 如果没有找到符合条件的元素,则返回 -1。
上一篇:js range
下一篇:js 引用类型
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站