当前位置:网站首页>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
}
})边栏推荐
- Stochastic process -- Markov chain
- vs2022scanf函数的使用,使用scanf的报错-返回值被忽略:解决·方法
- FPGA based electromagnetic ultrasonic pulse compression detection system paper + source file
- How to deal with unclear pictures? How to deal with color balance?
- [golang] how to clear slices gracefully
- 【Golang】使用Go语言操作etcd——配置中心
- Crise de 35 ans? Le volume intérieur est devenu synonyme de programmeur...
- Kotlin jetpack compose Tab的渲染 AnimatedVisibility的使用
- How to process the text of a picture into a table? Can the text in the picture be transferred to the document?
- 墨天轮访谈 | IvorySQL王志斌—IvorySQL,一个基于PostgreSQL的兼容Oracle的开源数据库
猜你喜欢

Add two factor authentication, not afraid of password disclosure, let alone 123456

重庆 奉节耀奎塔,建成后当地连中五名进士,是川江航运的安全塔

How to write a great online user manual in 7 steps

Use of the vs2022scanf function. An error is reported when using scanf - the return value is ignored: Solutions

Application of JDBC in performance test

墨天轮访谈 | IvorySQL王志斌—IvorySQL,一个基于PostgreSQL的兼容Oracle的开源数据库

ZABBIX monitoring - Aruba AP operation data

Development notes of wedding studio applet based on wechat applet

LeetCode 473. 火柴拼正方形

LeetCode 473. Match to square
随机推荐
Shell Scripting
【Golang】来几道题以加强Slice
小程序开发框架推荐
logstash启动 -r 参数
Are internal consultants and external consultants in SAP implementation projects difficult or successful?
Rstudio 1.4 software installation package and installation tutorial
[golang] follow the object pool sync Pool
[golang] reexamine closures from the perspective of go language
How do I open an account? Is it safe to open an account in Guohai Securities? What do you need to bring?
Is it safe for flush to open an account online? Is the Commission high
JS advanced programming version 4: generator learning
Open source SPL redefines OLAP server
How to open a domestic futures account? Which futures company is safer to open an account?
[golang] use go language to operate etcd - configuration center
35岁危机?内卷成程序员代名词了…
Implementing MySQL fuzzy search with node and express
LeetCode 1079. movable-type printing
Tupu software digital twin intelligent water service, breaking through the development dilemma of sponge City
Official announcement. Net 7 preview 5
The golden nine silver ten, depends on this detail, the offer obtains the soft hand!