当前位置:网站首页>JS to determine whether an element exists in the array (four methods)
JS to determine whether an element exists in the array (four methods)
2022-07-05 13:14:00 【Yan Shuangying】
Law 1 : utilize indexOf
There is no return -1, There is an index that returns the first occurrence
// js Check whether the array contains an element
// Law 1 indexOf
var arr = [100,20,50,58,6,69,36,45,78,66,45]
if(arr.indexOf(66)==-1){
console.log(" non-existent ")
}else{
console.log(" There is , The index is :",arr.indexOf(66))
}
Law two : utilize find
Its argument is a callback function , All array elements traverse the callback function in turn , Until you find out that the first return value is true The elements of , Then return the element , Otherwise return to 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(" There is ",index)
}
})
console.log(param)
lookup 45,find Will find out all that exists 45 And index
Law three : utilize some
some Methods are also used to detect whether there are elements that meet the conditions , If there is , Do not continue to retrieve the following elements , Go straight back to true, If it doesn't fit , Returns a false.
Usage and find be similar , It's just find Is to return the element that meets the condition ,some Back to a Boolean value , In terms of semantics , Whether to include the return Boolean value is more appropriate .
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)
Law four :includes
ES6 New array method , Used to detect whether the array contains an element , If include return true, Otherwise return to false, What's more powerful is , It can be detected directly NaN:
advantage Not to mention , There is no one in the simplest way , No callback , No complicated writing , One way to do it directly .
shortcoming It is not very friendly to support low version browsers
let arr = [100,20,50,58,6,69,36,45,78,66,45,NaN]
// Law four
let flag = arr.includes(1100)
let flag1 = arr.includes(NaN)
console.log(flag,flag1)
Recommended includes() Method , Convenient and quick , One step in place ~
边栏推荐
- 数据泄露怎么办?'华生·K'7招消灭安全威胁
- Discussion on error messages and API versions of SAP ui5 getsaplogonlanguage is not a function
- I'm doing open source in Didi
- MySQL 巨坑:update 更新慎用影响行数做判断!!!
- CloudCompare——点云切片
- Small case of function transfer parameters
- 蜀天梦图×微言科技丨达梦图数据库朋友圈+1
- Actual combat simulation │ JWT login authentication
- 946. 验证栈序列
- Sorry, we can't open xxxxx Docx, because there is a problem with the content (repackaging problem)
猜你喜欢
潘多拉 IOT 开发板学习(HAL 库)—— 实验7 窗口看门狗实验(学习笔记)
Hundred days to complete the open source task of the domestic database opengauss -- openguass minimalist version 3.0.0 installation tutorial
Introduction to the principle of DNS
百日完成国产数据库opengausss的开源任务--openGuass极简版3.0.0安装教程
Flutter 绘制波浪移动动画效果,曲线和折线图
初次使用腾讯云,解决只能使用webshell连接,不能使用ssh连接。
RHCSA9
峰会回顾|保旺达-合规和安全双驱动的数据安全整体防护体系
Asemi rectifier bridge hd06 parameters, hd06 pictures, hd06 applications
Word document injection (tracking word documents) incomplete
随机推荐
Lb10s-asemi rectifier bridge lb10s
APICloud Studio3 API管理与调试使用教程
ABAP editor in SAP segw transaction code
简单上手的页面请求和解析案例
Shu tianmeng map × Weiyan technology - Dream map database circle of friends + 1
SAP UI5 视图里的 OverflowToolbar 控件
Le rapport de recherche sur l'analyse matricielle de la Force des fournisseurs de RPA dans le secteur bancaire chinois en 2022 a été officiellement lancé.
无密码身份验证如何保障用户隐私安全?
Talk about my drawing skills in my writing career
Association modeling method in SAP segw transaction code
go 指针
Apicloud studio3 API management and debugging tutorial
Principle and configuration of RSTP protocol
MySQL splits strings for conditional queries
Concurrent performance test of SAP Spartacus with JMeter
Hundred days to complete the open source task of the domestic database opengauss -- openguass minimalist version 3.0.0 installation tutorial
SAP SEGW 事物码里的导航属性(Navigation Property) 和 EntitySet 使用方法
CF:A. The Third Three Number Problem【关于我是位运算垃圾这个事情】
Binder通信过程及ServiceManager创建过程
How can non-technical departments participate in Devops?