当前位置:网站首页>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
边栏推荐
- The ultimate version of the 13th simulation of the single chip microcomputer provincial competition of the Blue Bridge Cup
- Memo - about C # generating barcode
- Is Alipay wallet convenient to use?
- Data query language (DQL)
- Opencv map reading test -- error resolution
- Weekly recommended short videos: be alert to the confusion between "phenomena" and "problems"
- [source code analysis] NVIDIA hugectr, GPU version parameter server - (1)
- 隐私沙盒终于要来了
- PCL learning materials
- Regular expression
猜你喜欢
关于企业中台规划和 IT 架构微服务转型
Navicat Premium 15 永久破解和2021版本最新IDEA破解(亲测有效)
Sum of three numbers
Leetcode-141 circular linked list
PCL learning materials
Leetcode-160 intersecting linked list
The 13th simulation problem of the single chip microcomputer provincial competition of the Blue Bridge Cup
6款红黄黑榜摄像头评测:谁最安全?谁画质好?从此让你不再踩雷
What if the reliability coefficient is low? How to calculate the reliability coefficient?
Basic concepts of binary tree
随机推荐
力扣每日一题-第32天-1232. 缀点成线
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
C# SelfHost WebAPI (2)
Principal component calculation weight
Qt中的QFile读写文件操作
R语言使用epiDisplay包的dotplot函数通过点图的形式可视化不同区间数据点的频率、使用pch参数自定义指定点图数据点的形状
Navicat Premium 15 永久破解和2021版本最新IDEA破解(亲测有效)
Li Kou daily question - Day 32 -589 N × Preorder traversal of tree
Thread forced join, thread forced join application scenarios
Reading notes series "modern methods of C language programming" -- Chapter 2
主成分计算权重
Relational database management system of easyclick
What designs are needed in the architecture to build a general monitoring and alarm platform
创建您自己的NFT集合并发布一个Web3应用程序来展示它们(介绍)
Find all missing numbers in the array
[noip2015] jumping stone
磁盘的基本知识和基本命令
LiveData postValue会“丢”数据
MySQL connection tools
Lumiprobe 双功能交联剂丨Sulfo-Cyanine5 双-NHS 酯