当前位置:网站首页>如何判断数组中是否含有某个元素
如何判断数组中是否含有某个元素
2022-07-04 05:29:00 【_处女座程序员的日常】
使用findIndex
定义和用法:
findIndex() 方法返回传入一个测试条件(函数)符合条件的数组第一个元素位置。
有两点要注意:
- 当数组中的元素在测试条件时返回 true 时, findIndex() 返回符合条件的元素的索引位置,之后的值不会再调用执行函数。例子2就是一个很好的说明,即使后面的666和66大于50,但是它只找到99,就不会执行后面的循环了。
- 如果没有符合条件的元素返回 -1
例1:
let allList=[1,2,3,4,5];
let d = allList.findIndex(item=>item==5) //4.
例2:
var arr2 = [1,18,2,99,666,44,66];
var flag2 = arr2.findIndex(item => {
return item > 50;
});
console.log(flag2) // 3
find方法:找出元素中符合条件的元素
let allList=[{
id:1,name:'冬雨'},{
id:2,name:"冬梅"}];
let d = allList.find(item=>item.id==1)
console.log(d);// { id: 1, name: '冬雨' }
let allList=[{
id:1,name:'冬雨'},{
id:2,name:"冬梅"}];
let d = allList.find(item=>item.id==5)
console.log(d);//undefined
边栏推荐
猜你喜欢
2022年A特种设备相关管理(电梯)考试题模拟考试平台操作
Accidentally deleted the data file of Clickhouse, can it be restored?
如何使用postman实现简单的接口关联【增删改查】
Unity is connected to the weather system
Just do it with your hands 7 - * project construction details 2 - hook configuration
Graduation design of small programs -- small programs of food and recipes
[wechat applet] template and configuration (wxml, wxss, global and page configuration, network data request)
Programming example of stm32f1 and stm32subeide -74hc595 drives 4-bit 7-segment nixie tube
Upper computer software development - log information is stored in the database based on log4net
input显示当前选择的图片
随机推荐
Wechat applet +php realizes authorized login
Li Kou's 300th weekly match
[technology development -25]: integration technology of radio and television network, Internet, telecommunication network and power grid
Electronic components mall and data manual download website summary
c语言经典指针和数组笔试题解析
总线的基本概念
Unity2D--人物移动并转身
Programmers don't talk about morality, and use multithreading for Heisi's girlfriend
JS string splicing enhancement
Character types of C language
[untitled]
Letter meaning and parameter abbreviation of optical module Daquan
19. Framebuffer application programming
KMP match string
Programming example of stm32f1 and stm32subeide -74hc595 drives 4-bit 7-segment nixie tube
Automated testing selenium foundation -- webdriverapi
Online shrimp music will be closed in January next year. Netizens call No
1480. Dynamic sum of one-dimensional array
[interested reading] advantageous filtering modeling on long term user behavior sequences for click through rate pre
Simulink与Arduino串口通信