当前位置:网站首页>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 ~
边栏推荐
- Difference between avc1 and H264
- 简单上手的页面请求和解析案例
- STM32 and motor development (from architecture diagram to documentation)
- 无密码身份验证如何保障用户隐私安全?
- #从源头解决# 自定义头文件在VS上出现“无法打开源文件“XX.h“的问题
- Write macro with word
- MySQL giant pit: update updates should be judged with caution by affecting the number of rows!!!
- Concurrent performance test of SAP Spartacus with JMeter
- Flutter 绘制波浪移动动画效果,曲线和折线图
- 碎片化知识管理工具Memos
猜你喜欢

go 数组与切片

解决uni-app配置页面、tabBar无效问题

先写API文档还是先写代码?

SAP UI5 DynamicPage 控件介绍

解决 UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte 0xa2 in position 107

初次使用腾讯云,解决只能使用webshell连接,不能使用ssh连接。

Natural language processing series (I) introduction overview

《2022年中國銀行業RPA供應商實力矩陣分析》研究報告正式啟動

Pandora IOT development board learning (HAL Library) - Experiment 7 window watchdog experiment (learning notes)

A detailed explanation of ASCII code, Unicode and UTF-8
随机推荐
Detailed explanation of navigation component of openharmony application development
SAP SEGW 事物码里的导航属性(Navigation Property) 和 EntitySet 使用方法
函数的默认参数&函数参数的多种方法
Laravel document reading notes -mews/captcha use (verification code function)
APICloud Studio3 WiFi真机同步和WiFi真机预览使用说明
先写API文档还是先写代码?
There is no monitoring and no operation and maintenance. The following is the commonly used script monitoring in monitoring
Leetcode20. Valid parentheses
Pandora IOT development board learning (HAL Library) - Experiment 7 window watchdog experiment (learning notes)
AVC1与H264的区别
Rocky基础命令3
Cf:a. the third three number problem
How to choose note taking software? Comparison and evaluation of notion, flowus and WOLAI
无密码身份验证如何保障用户隐私安全?
It's too convenient. You can complete the code release and approval by nailing it!
将函数放在模块中
A specific example of ABAP type and EDM type mapping in SAP segw transaction code
Alibaba cloud SLB load balancing product basic concept and purchase process
go 数组与切片
The solution of outputting 64 bits from printf format%lld of cross platform (32bit and 64bit)