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