当前位置:网站首页>js判断数组中是否存在某个元素(四种方法)
js判断数组中是否存在某个元素(四种方法)
2022-07-05 12:56:00 【燕双鹰...】
法一:利用indexOf
不存在返回-1,存在返回第一次出现的索引
// js检查数组中是否包含某个元素
// 法一 indexOf
var arr = [100,20,50,58,6,69,36,45,78,66,45]
if(arr.indexOf(66)==-1){
console.log("不存在")
}else{
console.log("存在,索引是:",arr.indexOf(66))
}
法二:利用find
它的参数是一个回调函数,所有数组元素依次遍历该回调函数,直到找出第一个返回值为true的元素,然后返回该元素,否则返回undefined。
var arr = [100,20,50,58,6,69,36,45,78,66,45]
arr.find(function(value,index,arr){
if(value==45){
console.log("存在",index)
}
})
console.log(param)
查找45,find会找出所有存在的45以及索引
法三:利用some
some方法同样用于检测是否有满足条件的元素,如果有,则不继续检索后面的元素,直接返回true,如果都不符合,则返回一个false。
用法与find相似,只是find是返回满足条件的元素,some返回的是一个Boolean值,从语义化来说,是否包含返回布尔值更贴切。
let arr = [100,20,50,58,6,69,36,45,78,66,45]
// some
let result = arr.some(ele => ele === 45) //true
if (result) {
//do something...
};
console.log(result)
法四:includes
ES6新增的数组方法,用于检测数组是否包含某个元素,如果包含返回true,否则返回false,比较厉害的是,能直接检测NaN:
优点 就不用说了,最简单的做法没有之一,不用回调,不用复杂的写法,一个方法直接搞定。
缺点 是低版本浏览器支持不是很友好
let arr = [100,20,50,58,6,69,36,45,78,66,45,NaN]
// 法四
let flag = arr.includes(1100)
let flag1 = arr.includes(NaN)
console.log(flag,flag1)
推荐使用includes()方法,方便快捷,一步到位~
边栏推荐
- [cloud native] use of Nacos taskmanager task management
- Talking about fake demand from takeout order
- 跨平台(32bit和64bit)的 printf 格式符 %lld 输出64位的解决方式
- Install rhel8.2 virtual machine
- 946. Verify stack sequence
- Sorry, we can't open xxxxx Docx, because there is a problem with the content (repackaging problem)
- 逆波兰表达式
- Hundred days to complete the open source task of the domestic database opengauss -- openguass minimalist version 3.0.0 installation tutorial
- Overflow toolbar control in SAP ui5 view
- A small talk caused by the increase of sweeping
猜你喜欢
开发者,云原生数据库是未来吗?
SAP UI5 DynamicPage 控件介绍
Small case of function transfer parameters
简单上手的页面请求和解析案例
解决uni-app配置页面、tabBar无效问题
解决 UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte 0xa2 in position 107
PyCharm安装第三方库图解
DataPipeline双料入选中国信通院2022数智化图谱、数据库发展报告
Asemi rectifier bridge hd06 parameters, hd06 pictures, hd06 applications
946. 验证栈序列
随机推荐
Natural language processing from Xiaobai to proficient (4): using machine learning to classify Chinese email content
A deep long article on the simplification and acceleration of join operation
Reflection and imagination on the notation like tool
将函数放在模块中
事务的基本特性和隔离级别
Talk about my drawing skills in my writing career
RHCSA3
关于 Notion-Like 工具的反思和畅想
Principle and configuration of RSTP protocol
SAP ui5 objectpagelayout control usage sharing
自然语言处理系列(一)入门概述
Word document injection (tracking word documents) incomplete
函数传递参数小案例
A specific example of ABAP type and EDM type mapping in SAP segw transaction code
ABAP editor in SAP segw transaction code
[cloud native] event publishing and subscription in Nacos -- observer mode
程序员成长第八篇:做好测试工作
SAP UI5 DynamicPage 控件介紹
STM32 and motor development (from architecture diagram to documentation)
About the single step debugging of whether SAP ui5 floating footer is displayed or not and the benefits of using SAP ui5