当前位置:网站首页>How to determine whether an array contains an element
How to determine whether an array contains an element
2022-07-04 05:42:00 【_ Virgo programmer's daily life】
Use findIndex
Definition and Usage :
findIndex() Method returns a test condition passed in ( function ) The first element position of the qualified array .
There are two points to pay attention to :
- When an element in an array tests a condition, it returns true when , findIndex() Returns the index position of a qualified element , Subsequent values do not call the execute function . Example 2 Is a good illustration , Even in the back 666 and 66 Greater than 50, But it only found 99, The following loop will not be executed .
- If no eligible element returns -1
example 1:
let allList=[1,2,3,4,5];
let d = allList.findIndex(item=>item==5) //4.
example 2:
var arr2 = [1,18,2,99,666,44,66];
var flag2 = arr2.findIndex(item => {
return item > 50;
});
console.log(flag2) // 3
find Method : Find out the qualified elements in the elements
let allList=[{
id:1,name:' Winter rain '},{
id:2,name:" Wintersweet "}];
let d = allList.find(item=>item.id==1)
console.log(d);// { id: 1, name: ' Winter rain ' }
let allList=[{
id:1,name:' Winter rain '},{
id:2,name:" Wintersweet "}];
let d = allList.find(item=>item.id==5)
console.log(d);//undefined
边栏推荐
猜你喜欢
LM small programmable controller software (based on CoDeSys) note XXI: error 3703
基于单片机的太阳能杀虫系统
BeanFactoryPostProcessor 与 BeanPostProcessor 相关子类概述
Configure cross compilation tool chain and environment variables
VB.net 简单的处理图片,黑白(类库——7)
[excel] PivotChart
拓扑排序和关键路径的图形化显示
【QT】制作MyComboBox点击事件
Supplement the JS of a video website to decrypt the video
js如何将秒转换成时分秒显示
随机推荐
XII Golang others
Programmers don't talk about morality, and use multithreading for Heisi's girlfriend
BUU-Pwn-test_ your_ nc
transformer坑了多少算力
px em rem的区别
ANSYS command
input显示当前选择的图片
[interested reading] advantageous filtering modeling on long term user behavior sequences for click through rate pre
【QT】制作MyComboBox点击事件
With the advent of the IP era, how can E-sports hotels take advantage of the "east wind" of games?
SQL injection - injection based on MSSQL (SQL Server)
win10清除快速访问-不留下痕迹
Enterprise level log analysis system elk (if things backfire, there must be other arrangements)
left_and_right_net正常版本
Kubernets first meeting
[QT] create mycombobox click event
19. Framebuffer application programming
Nodejs learning document
Graduation design of small programs -- small programs of food and recipes
(4) Canal multi instance use