findIndex JavaScript · 数组方法
返回第一个满足测试条件的元素索引,找不到返回 -1。
怎么用
数组.findIndex((元素, 索引, 数组) => 条件)
小例子
console.log([0, 5, 10].findIndex(x => x > 0)); console.log([0, -1].findIndex(x => x > 0));
运行结果
1 -1
提醒:与 find 类似,但返回索引。
官方文档:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex
在代码里遇到不认识的词?打开代码点读机,点一下就懂 → ← JavaScript全部词条