当前位置:网站首页>JS five methods to judge whether a certain value exists in an array
JS five methods to judge whether a certain value exists in an array
2022-06-23 20:33:00 【It workers】
1.javascript Of indexOf() Method
var arr_data = [1,2,3];
arr_data.indexOf(1); // If there is a subscript for the return value , There is no return -1
2.jquery Of $.inArray() Method
$.inArray(1, arr_data); // If there is a subscript for the return value , There is no return -1
3.arr.find()
Of the array instance find() Used to find the first array element that matches the condition . 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.
Be careful :find() For an empty array , Functions do not execute .find() It doesn't change the original value of the array
arr.find(function(value) {
if(value === Value to find ) {
// Contains the element
}})4.arr.findIndex() Returns the position of the first eligible array element , If all the elements do not meet the conditions , Then return to -1.
Be careful :find(),findIndex() Make up for index Deficiency :( Judgment NAN)
[NaN].indexOf(NaN) // -1 [NaN].findIndex(y => Object.is(NaN, y))// 0
5.for Circulation and if Judge
var arr = [1, 5, 10, 15];
// Tradition for
for(let i=0; i<arr.length; i++) {
if(arr[i] === Find value ) {
// Contains the element
}
}
// for...of
for(v of arr) {
if(v === Find value ) {
// Contains the element
}
}
//forEach
arr.forEach(v=>{
if(v === Find value ) {
// Contains the element
}
})边栏推荐
- Eight misunderstandings, broken one by one (final): the cloud is difficult to expand, the customization is poor, and the administrator will lose control?
- Ugeek's theory 𞓜 application and design of observable hyperfusion storage system
- Syntax of SQL union query (inline, left, right, and full)
- 【Golang】使用Go语言操作etcd——配置中心
- Implementation of microblog system based on SSM
- 【Golang】快速复习指南QuickReview(八)——goroutine
- Kotlin jetpack compose Tab的渲染 AnimatedVisibility的使用
- How PostgreSQL quickly locate blocking SQL
- SQL聯合查詢(內聯、左聯、右聯、全聯)的語法
- Emmet syntax specification
猜你喜欢

UGeek大咖说 | 可观测之超融合存储系统的应用与设计

每日刷题记录 (二)

Tcp/udp Fundamentals

Interview with Mo Tianlun | ivorysql wangzhibin - ivorysql, an Oracle compatible open source database based on PostgreSQL

Activity registration | introduction to mongodb 5.0 sequential storage features

Why is only one value displayed on your data graph?

增加双因素认证,不惧密码泄露,更不惧123456

JS advanced programming version 4: generator learning

Can the biggest gamefi crash victim survive the bear market in May| May Monthly Report

LeetCode 260. Number III that appears only once
随机推荐
The evolution of the "Rainbow Bridge" middleware platform for the acquisition database based on shardingsphere
I came from a major, so I didn't want to outsource
Add two factor authentication, not afraid of password disclosure, let alone 123456
[golang] quick review guide quickreview (III) - Map
What cloud disk types does Tencent cloud provide? What are the characteristics of cloud disk service?
Tupu software digital twin intelligent water service, breaking through the development dilemma of sponge City
3000 frame animation illustrating why MySQL needs binlog, redo log and undo log
How PostgreSQL quickly locate blocking SQL
【Golang】使用Go语言操作etcd——配置中心
Is Huishang futures trading software formal? How to download safely?
【Golang】怎么实现Go程序的实时热更新
Stochastic process -- Markov chain
Gaussdb (DWS) database intelligent monitoring operation and maintenance service - node monitoring indicators
Official announcement. Net 7 preview 5
35岁危机?内卷成程序员代名词了…
Kubernetes 资源拓扑感知调度优化
Is it safe for flush to open an account online? Is the Commission high
Emmet syntax specification
[golang] quick review guide quickreview (VII) -- Interface
MySQL时间函数的运用,简单问题