JavaScript Array findIndex 方法

JavaScript About 361 words

注意

  • 存在则返回第一个符合条件的元素的索引,索引从0开始计数。
  • 不存在则返回-1

引用类型

const arr = [
    {amount: 39.9, name: "product c"},
    {amount: 19.9, name: "product a"},
    {amount: 29.9, name: "product b"},
];

const index = arr.findIndex(item => item.amount === 39.9);
console.log(index);

输出

0

基础类型

const index2 = [2, 4, 6, 8, 10].findIndex(item => item > 6);
console.log(index2)

输出

3
Views: 10 · Posted: 2025-11-27

————        END        ————

Give me a Star, Thanks:)

https://github.com/fendoudebb/LiteNote

扫描下方二维码关注公众号和小程序↓↓↓

扫描下方二维码关注公众号和小程序↓↓↓
Today In History
Browsing Refresh