// 使用 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 findindex
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站