当前位置:网站首页>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
边栏推荐
- Three.js学习-相机Camera的基本操作(了解向)
- Write it down once Net travel management background CPU Explosion Analysis
- Leetcode-128 最长连续序列
- 如何运营好技术相关的自媒体?
- R语言使用epiDisplay包的followup.plot函数可视化多个ID(病例)监测指标的纵向随访图、使用n.of.lines参数指定显示的病例数
- 斯坦福、Salesforce|MaskViT:蒙面视觉预训练用于视频预测
- R语言使用epiDisplay包的dotplot函数通过点图的形式可视化不同区间数据点的频率、使用pch参数自定义指定点图数据点的形状
- Cloud computing - make learning easier
- How does factor analysis calculate weights?
- Operating system interview assault
猜你喜欢

After studying 11 kinds of real-time chat software, I found that they all have these functions

Implement a Prometheus exporter

Calculation of intersection of two line segments

创建您自己的NFT集合并发布一个Web3应用程序来展示它们(介绍)

主成分之综合竞争力案例分析

Li Kou daily question - Day 32 -589 N × Preorder traversal of tree

Leetcode-83 delete duplicate elements in the sorting linked list

Navicat premium 15 permanent cracking and 2021 latest idea cracking (valid for personal testing)

540. Single element in ordered array

Weekly recommended short videos: be alert to the confusion between "phenomena" and "problems"
随机推荐
Leetcode-160 intersecting linked list
Five degrees easy chain enterprise app is newly upgraded
磁盘的基本知识和基本命令
Sanfeng cloud 0215 I often use
Lumiprobe bifunctional crosslinker sulfo cyanine 5 bis NHS ester
What if the reliability coefficient is low? How to calculate the reliability coefficient?
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
Navicat premium 15 permanent cracking and 2021 latest idea cracking (valid for personal testing)
The ultimate version of the 13th simulation of the single chip microcomputer provincial competition of the Blue Bridge Cup
R language uses the transmute function of dplyr package to calculate the moving window mean value of the specified data column in dataframe data, and uses ggplot2 package to visualize the line graph b
关于企业中台规划和 IT 架构微服务转型
The 13th simulation problem of the single chip microcomputer provincial competition of the Blue Bridge Cup
Depth first search - DFS (burst search)
What designs are needed in the architecture to build a general monitoring and alarm platform
《Go题库·16》读写锁底层是怎么实现的
Mise en place d'une plate - forme générale de surveillance et d'alarme, quelles sont les conceptions nécessaires dans l'architecture?
创建您自己的NFT集合并发布一个Web3应用程序来展示它们(介绍)
字节跳动数据平台技术揭秘:基于 ClickHouse 的复杂查询实现与优化
code
Lumiprobe 双功能交联剂丨Sulfo-Cyanine5 双-NHS 酯