当前位置:网站首页>ES6数组方法find()、findIndex()的总结「建议收藏」
ES6数组方法find()、findIndex()的总结「建议收藏」
2022-07-01 18:35:00 【全栈程序员站长】
大家好,又见面了,我是你们的朋友全栈君。
本文主要讲解ES6数组方法find()与findIndex(),关于JS的更多数组方法,可参考以下:
②ES5新增数组方法(例:map()、indexOf()、filter()等)
③ES6新增字符串扩张方法includes()、startsWith()、endsWith()
1. find()
- 该方法主要应用于查找
第一个符合条件的数组元素,即返回通过测试(函数内判断)的数组的第一个元素的值。 - 它的参数是一个回调函数,为数组中的每个元素都调用一次函数执行。在回调函数中可以写你要查找元素的条件,当条件成立为
true时,返回该元素,之后的值不会再调用执行函数。如果没有符合条件的元素,返回值为undefined。
例: ① 以下代码在myArr数组中查找元素值大于5的元素,找到后立即返回,并不会继续往下执行。返回的结果为查找到的元素:
const myArr=[1,2,3,4,5,6,7,8,9];
var v=myArr.find(value=>value>5);
console.log(v);结果:
② 如果把条件改为>10,没有符合元素,则返回undefined:
const myArr=[1,2,3,4,5,6,7,8,9];
var v=myArr.find(value=>value>10);
console.log(v); 结果:
③ 它的回调函数有三个参数。value:当前的数组元素。index:当前索引值。arr:被查找的数组。
例: 查找索引值为5的元素,结果显示6:
const myArr=[1,2,3,4,5,6];
var v=myArr.find((value,index,arr)=>{
return index===5;
});
console.log(v);结果:
注意:
- find() 对于空数组,函数是不会执行的。
- find() 并没有改变数组的原始值。
2. findIndex()
- findIndex() 方法返回传入一个测试条件(函数)符合条件的数组
第一个元素位置。 - 当数组中的元素在测试条件时返回
true时, findIndex() 返回符合条件的元素的索引位置(注:find()返回的是元素),之后的值不会再调用执行函数。如果没有符合条件的元素返回-1(注:find()返回的是undefined)。 - findIndex()与find()的
使用方法相同,findIndex()当中的回调函数也是接收三个参数,与find()相同。 - findIndex()方法实现是通过循环遍历查找。应用场景广泛,可以查找大于等于小于,表达式可以随便写。实际上相当于一个for循环,只不过找到了你不需要自己退出。
语法:
array.findIndex(function(currentValue, index, arr), thisValue);例①:
const myArr=[
{
id:1,
Name:"张三"
},
{
id:2,
Name:"李四"
},
{
id:3,
Name:"王五"
},
{
id:4,
Name:"赵六"
}
];
var i0=myArr.findIndex((value)=>value.id==1);
console.log(i0);
var i1=myArr.findIndex((value)=>value.id==2);
console.log(i1);
var i2=myArr.findIndex((value)=>value.id==3);
console.log(i2);
var i3=myArr.findIndex((value)=>value.id==4);
console.log(i3);
var i4=myArr.findIndex((value)=>value.id==5);
console.log(i4); 结果:
例②:
const myArr = [1,2,3,4,5,6,7,8,9];
function bigNum(ele){
return ele > 6;
}
console.log(myArr.findIndex(bigNum));结果(也就是数组中第一个大于6的数,即“7”所在位置的索引):
例③:可以用来返回符合大于输入框中数字的数组索引
var ages = [2,4,6,8,10];
function checkAdult(age) {
return age >= document.getElementById("ageToCheck").value;
}
function myFunction() {
document.getElementById("demo").innerHTML = ages.findIndex(checkAdult);
}注意:
- findIndex() 对于空数组,函数是不会执行的。
- findIndex() 并没有改变数组的原始值。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/130822.html原文链接:https://javaforall.cn
边栏推荐
- Bernoulli distribution (a discrete distribution)
- 用GSConv+Slim Neck改进Yolov5,将性能提升到极致!
- Thread forced join, thread forced join application scenarios
- Blue Bridge Cup real problem: word analysis
- The R language cartools package divides the data, the scale function scales the data, the KNN function of the class package constructs the k-nearest neighbor classifier, and the table function calcula
- Rust language - cargo, crates io
- Relational database management system of easyclick
- GAMES202作业0-环境搭建过程&解决遇到的问题
- Cloud computing - make learning easier
- Lumiprobe非荧光炔烃丨EU(5-乙炔基尿苷)
猜你喜欢

Using OpenSSL encryption to rebound shell traffic

Calculation of intersection of two line segments

Privacy sandbox is finally coming

Must see, time series analysis

每周推薦短視頻:警惕“現象”與“問題”相互混淆

搭建一個通用監控告警平臺,架構上需要有哪些設計

Lumiprobe bifunctional crosslinker sulfo cyanine 5 bis NHS ester

Unity learning fourth week

宏观视角看抖音全生态

About enterprise middle office planning and it architecture microservice transformation
随机推荐
R language ggplot2 visualization: gganimate creates a dynamic histogram animation (GIF), and displays the histogram and enter step by step along a given dimension in the animation_ Growth function and
Facebook聊单,SaleSmartly有妙招!
3、《创建您自己的NFT集合并发布一个Web3应用程序来展示它们》在本地铸造 NFT
Operating system interview assault
Five degrees easy chain enterprise app is newly upgraded
R language uses the aggregate function of epidisplay package to divide numerical variables into different subsets based on factor variables, and calculate the summary statistics of each subset
Lumiprobe bifunctional crosslinker sulfo cyanine 5 bis NHS ester
[Chongqing Guangdong education] basic psychology reference materials of Tianjin Normal University
主成分计算权重
Navicat Premium 15 永久破解和2021版本最新IDEA破解(亲测有效)
解决方案:可以ping别人,但是别人不能ping我
About enterprise middle office planning and it architecture microservice transformation
R language epidisplay package ordinal or. The display function obtains the summary statistical information of the ordered logistic regression model (the odds ratio and its confidence interval correspo
R语言ggplot2可视化:可视化折线图、使用labs函数为折线图添加自定义的Y轴标签信息(customize y axis label)
如何在自有APP内实现小程序实现连麦直播
Memo - about C # generating barcode for goods
关于企业中台规划和 IT 架构微服务转型
Solution: you can ping others, but others can't ping me
Privacy sandbox is finally coming
Implementation of converting PCM file to WAV