当前位置:网站首页>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 ~
边栏推荐
- Small case of function transfer parameters
- Write macro with word
- DataPipeline双料入选中国信通院2022数智化图谱、数据库发展报告
- [deep learning paper notes] hnf-netv2 for segmentation of brain tumors using multimodal MR imaging
- 逆波兰表达式
- 潘多拉 IOT 开发板学习(HAL 库)—— 实验7 窗口看门狗实验(学习笔记)
- #从源头解决# 自定义头文件在VS上出现“无法打开源文件“XX.h“的问题
- Lb10s-asemi rectifier bridge lb10s
- 先写API文档还是先写代码?
- SAP UI5 DynamicPage 控件介紹
猜你喜欢

Lb10s-asemi rectifier bridge lb10s

Natural language processing series (I) introduction overview

Asemi rectifier bridge hd06 parameters, hd06 pictures, hd06 applications

MSTP and eth trunk

无密码身份验证如何保障用户隐私安全?

聊聊异步编程的 7 种实现方式

The Research Report "2022 RPA supplier strength matrix analysis of China's banking industry" was officially launched

Binder通信过程及ServiceManager创建过程

Solve Unicode decodeerror: 'GBK' codec can't decode byte 0xa2 in position 107

Overflow toolbar control in SAP ui5 view
随机推荐
How to protect user privacy without password authentication?
Flutter 绘制波浪移动动画效果,曲线和折线图
leetcode:221. 最大正方形【dp状态转移的精髓】
Introduction to sap ui5 flexiblecolumnlayout control
CF:A. The Third Three Number Problem【关于我是位运算垃圾这个事情】
一文详解ASCII码,Unicode与utf-8
逆波兰表达式
[深度学习论文笔记]使用多模态MR成像分割脑肿瘤的HNF-Netv2
It's too convenient. You can complete the code release and approval by nailing it!
How to realize batch sending when fishing
UnicodeDecodeError: ‘utf-8‘ codec can‘t decode byte 0xe6 in position 76131: invalid continuation byt
Datapipeline was selected into the 2022 digital intelligence atlas and database development report of China Academy of communications and communications
[cloud native] use of Nacos taskmanager task management
leetcode:221. Maximum square [essence of DP state transition]
阿里云SLB负载均衡产品基本概念与购买流程
go map
go 数组与切片
uni-app开发语音识别app,讲究的就是简单快速。
《2022年中国银行业RPA供应商实力矩阵分析》研究报告正式启动
SAE international strategic investment geometry partner