当前位置:网站首页>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
边栏推荐
- Introduction to easyclick database
- After studying 11 kinds of real-time chat software, I found that they all have these functions
- Leetcode-83 delete duplicate elements in the sorting linked list
- Lumiprobe biomolecular quantification - qudye Protein Quantification Kit
- Three.js学习-相机Camera的基本操作(了解向)
- Blue Bridge Cup real topic: the shortest circuit
- 为什么独立站卖家都开始做社交媒体营销?原来客户转化率能提高这么多!
- Leetcode-83 删除排序链表中重复的元素
- 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
- 隐私沙盒终于要来了
猜你喜欢
Navicat premium 15 permanent cracking and 2021 latest idea cracking (valid for personal testing)
OpenAI|视频预训练 (VPT):基于观看未标记的在线视频的行动学习
PCL learning materials
3、《创建您自己的NFT集合并发布一个Web3应用程序来展示它们》在本地铸造 NFT
Basic knowledge and commands of disk
Leetcode-83 delete duplicate elements in the sorting linked list
Find all missing numbers in the array
Why do independent website sellers start to do social media marketing? The original customer conversion rate can be improved so much!
1、《创建您自己的NFT集合并发布一个Web3应用程序来展示它们》什么是NFT
12. Design of power divider for ads usage record
随机推荐
如何在自有APP内实现小程序实现连麦直播
Popular science: what does it mean to enter the kernel state?
Halcon图片标定,使得后续图片处理过后变成与模板图片一样
R语言使用epiDisplay包的dotplot函数通过点图的形式可视化不同区间数据点的频率、使用pch参数自定义指定点图数据点的形状
golang 错误处理
Blue Bridge Cup real problem: word analysis
A wonderful time to buy and sell stocks
MySQL connection tools
Must see, time series analysis
[source code analysis] model parallel distributed training Megatron (5) -- pipestream flush
Salesmartly has some tricks for Facebook chat!
How to change guns for 2D characters
[image denoising] matlab code for removing salt and pepper noise based on fast and effective multistage selective convolution filter
A database editing gadget that can edit SQLite database. SQLite database replaces fields. SQL replaces all values of a field in the database
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
Lumiprobe bifunctional crosslinker sulfo cyanine 5 bis NHS ester
Mysql database design
LeetCode-21合并两个有序链表
Financial judgment questions
Navicat premium 15 permanent cracking and 2021 latest idea cracking (valid for personal testing)