我的github
posts - 3243,  comments - 42,  views - 158万

indexOf:在JavaScript中,你可以使用indexOf()方法来查找数组中元素的位置。如果元素不存在于数组中,indexOf()会返回-1

let index = array.indexOf('x')
if(index!=-1){
  //...
}

findIndex:如果你需要查找的是复杂对象数组,你可能需要自定义一个查找函数,使用findIndex()

let objectsArray = [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }];
let index = objectsArray.findIndex(obj => obj.id === 2); // 查找对象id为2的位置
 
if (index !== -1) {
  console.log(`对象id为2的位置是: ${index}`);
} else {
  console.log('对象id为2不在数组中');
}

参考:百度AI

lastIndexOf:

 

posted on   XiaoNiuFeiTian  阅读(114)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
历史上的今天:
2022-12-06 vscode如何设置为左边编辑器右边浏览器
2022-12-06 mysql insert into table1(att1,att2,...) [values] select att1,att2,... from table2
2021-12-06 javascript keycode大全&addeventhandler键盘监听事件
2020-12-06 javascript发送post请求(json格式)
2019-12-06 物体检测和边缘检测(目标识别和图像分割)
2016-12-06 单目视觉slam(OrbSlam:A Versatile and Accurate Monocular SLAM System)
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示