当前位置:网站首页>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
}
})边栏推荐
- Application of MySQL time function, simple problem
- 【Golang】使用Go语言操作etcd——配置中心
- 实现vscode写markdown文档+图片自动上传至腾讯云cos
- Official announcement. Net 7 preview 5
- 【Golang】类型转换归纳总结
- Kinsoku Jikou Desu Sina stock interface change
- The "open source star picking program" container pulls private images from harbor, which is a necessary skill for cloud native advanced technology
- LeetCode 260. Number III that appears only once
- How do I open an account? Is it safe to open an account in Guohai Securities? What do you need to bring?
- 【Golang】快速复习指南QuickReview(八)——goroutine
猜你喜欢

每日刷题记录 (二)

GL Studio 5 installation and experience

Zabbix监控- Aruba AP运行数据

Daily question brushing record (II)

FPGA based electromagnetic ultrasonic pulse compression detection system paper + source file

SQL聯合查詢(內聯、左聯、右聯、全聯)的語法

Application of JDBC in performance test

Kubernetes 资源拓扑感知调度优化

Why is only one value displayed on your data graph?

I came from a major, so I didn't want to outsource
随机推荐
Hotline salon issue 26 - cloud security session
TCP/UDP基本原理
数字化采购转型解决方案:SaaS采购管理平台推进企业阳光采购
[golang] how to clear slices gracefully
如何利用数仓创建时序表
【Golang】快速复习指南QuickReview(七)——interface
What are the useful personnel management software? Personnel management system software ranking!
【Golang】怎么实现Go程序的实时热更新
Tupu software digital twin intelligent water service, breaking through the development dilemma of sponge City
Can the biggest gamefi crash victim survive the bear market in May| May Monthly Report
Technology sharing | wvp+zlmediakit realizes streaming playback of camera gb28181
The golden nine silver ten, depends on this detail, the offer obtains the soft hand!
Want to open an account, is it safe to open an account on the stock Internet? Will the funds be cheated away?
Is it safe for Huatai Securities to open an account online for securities companies with low handling fees and commissions
logstash启动 -r 参数
[golang] some questions to strengthen slice
Realize vscode to write markdown documents + pictures to be automatically uploaded to Tencent cloud cos
[golang] reexamine closures from the perspective of go language
Check four WiFi encryption standards: WEP, WPA, WPA2 and WPA3
Deep learning of handlebar handwriting (15): building your own corpus on hugging face