当前位置:网站首页>ES6 summary "suggestions collection" of array methods find(), findindex()
ES6 summary "suggestions collection" of array methods find(), findindex()
2022-07-01 18:48:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm your friend, Quan Jun .
This article mainly explains ES6 Array methods find()
And findIndex()
, About JS More array methods for , Refer to the following :
①JavaScript Built in objects -Array
②ES5 New array method ( example :map()、indexOf()、filter() etc. )
③ES6 New string expansion method includes()、startsWith()、endsWith()
1. find()
- This method is mainly used to find
first
Qualified array elements , That is, return to pass the test ( In function judgment ) The value of the first element of the array . - Its argument is a callback function , Call the function once for each element in the array to execute . In the callback function, you can write the condition you want to find the element , When the condition is
true
when , Return the element , The value afterCan't
Then call the execution function .without
Elements that meet the conditions , Return valueby undefined
.
example : ① The following code is myArr The value of the search element in the array is greater than 5 The elements of , Return as soon as found , It's not going to continue . The returned result is the found element :
const myArr=[1,2,3,4,5,6,7,8,9];
var v=myArr.find(value=>value>5);
console.log(v);
result :
② If the condition is changed to >10, No matching element , Then return to undefined:
const myArr=[1,2,3,4,5,6,7,8,9];
var v=myArr.find(value=>value>10);
console.log(v);
result :
③ Its callback function has three parameters .value: The current array element .index: Current index value .arr: The array to be looked up .
example : The lookup index value is 5 The elements of , Results show 6:
const myArr=[1,2,3,4,5,6];
var v=myArr.find((value,index,arr)=>{
return index===5;
});
console.log(v);
result :
Be careful :
- find() For an empty array , Functions do not execute .
- find() It doesn't change the original value of the array .
2. findIndex()
- findIndex() Method returns a test condition passed in ( function ) Qualified array
first
Element location . - When an element in an array tests a condition, it returns
true
when , findIndex() returnThe index position of the eligible element
(notes :find() What is returned is the element
), The value afterCan't
Then call the execution function . IfNo,
The qualified element returns-1
(notes :find() The return is undefined
). - findIndex() And find() Of
Use the same way
,findIndex() The callback function also receives three parameters , And find() identical . - findIndex() The method is implemented by looping through the search . Wide application scenarios , You can find greater than or equal to less than , Expressions can be written casually . It's actually equivalent to a for loop , It's just that you don't need to quit when you find it .
grammar :
array.findIndex(function(currentValue, index, arr), thisValue);
example ①:
const myArr=[
{
id:1,
Name:" Zhang San "
},
{
id:2,
Name:" Li Si "
},
{
id:3,
Name:" Wang Wu "
},
{
id:4,
Name:" Zhao Liu "
}
];
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);
result :
example ②:
const myArr = [1,2,3,4,5,6,7,8,9];
function bigNum(ele){
return ele > 6;
}
console.log(myArr.findIndex(bigNum));
result ( That is, the first one in the array is greater than 6 Number of numbers , namely “7” The index of the location ):
example ③: It can be used to return an array index that matches a number larger than the number in the input box
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);
}
Be careful :
- findIndex() For an empty array , Functions do not execute .
- findIndex() It doesn't change the original value of the array .
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/130822.html Link to the original text :https://javaforall.cn
边栏推荐
- app发版后的缓存问题
- 12种数据量纲化处理方式
- 研究了11种实时聊天软件,我发现都具备这些功能…
- Privacy sandbox is finally coming
- Roll out! Enlightenment!
- How to find the optimal learning rate
- The 13th simulation problem of the single chip microcomputer provincial competition of the Blue Bridge Cup
- Technology implementation and Architecture Practice
- R语言epiDisplay包ordinal.or.display函数获取有序logistic回归模型的汇总统计信息(变量对应的优势比及其置信区间、以及假设检验的p值)、write.csv函数保存csv
- How to find customers for investment attraction in industrial parks
猜你喜欢
A database editing gadget that can edit SQLite database. SQLite database replaces fields. SQL replaces all values of a field in the database
每周推荐短视频:警惕“现象”与“问题”相互混淆
Leetcode-128 最长连续序列
1、《创建您自己的NFT集合并发布一个Web3应用程序来展示它们》什么是NFT
Lumiprobe lumizol RNA extraction reagent solution
LiveData postValue会“丢”数据
The 13th simulation problem of the single chip microcomputer provincial competition of the Blue Bridge Cup
540. Single element in ordered array / 1684 Count the number of consistent strings
搭建一个通用监控告警平台,架构上需要有哪些设计
Search 2D matrix 2
随机推荐
必看,时间序列分析
Qt中的QFile读写文件操作
记一次 .NET 差旅管理后台 CPU 爆高分析
解决方案:可以ping别人,但是别人不能ping我
6款红黄黑榜摄像头评测:谁最安全?谁画质好?从此让你不再踩雷
力扣每日一题-第32天-1232. 缀点成线
Navicat premium 15 permanent cracking and 2021 latest idea cracking (valid for personal testing)
创建您自己的NFT集合并发布一个Web3应用程序来展示它们(介绍)
宏观视角看抖音全生态
C# SelfHost WebAPI (2)
R语言使用epiDisplay包的tableStack函数制作统计汇总表格(基于目标变量分组的描述性统计、假设检验等)、不设置by参数则计算数据框指定数据列范围的基础描述性统计信息
实现一个Prometheus exporter
实例讲解将Graph Explorer搬上JupyterLab
12 data dimensioning processing methods
lefse分析
R语言使用epiDisplay包的dotplot函数通过点图的形式可视化不同区间数据点的频率、使用pch参数自定义指定点图数据点的形状
Livedata postvalue will "lose" data
Sanfeng cloud 0215 I often use
Excel之VBA简单宏编程
[source code analysis] NVIDIA hugectr, GPU version parameter server - (1)