当前位置:网站首页>ES6 - map (), filter (), reduce (), foreach (), the most commonly used array methods,
ES6 - map (), filter (), reduce (), foreach (), the most commonly used array methods,
2022-07-18 21:43:00 【ncj0612】
Catalog
1,map() Method
map() Method will execute a function on each element of the array to create a New array , Will not execute functions that are not worth it , And the original array will not be changed
map () The method has three arguments
- All elements of the array
- Index value of array element ( Subscript )
- The array itself
for example :
Add 3;
const arr = [1,6,7,3,4,5];
const newarr=arr.map((item,index,array)=>{
console.log(' Elements in an array '+item);
console.log(' Index of each array element '+index);
console.log(' The array itself '+array);
return item +3 ;
})
console.log(newarr);design sketch

Be careful : If there is only one parameter in the callback function , Sure Omit Index parameters and Array parameters
2,filter() Method
filter() Function is a filter function , Filter out the qualified functions And return and create a new array , It doesn't change the original array , Nor will it perform functions on empty arrays
filter() function And map() Function also has three parameters
- All elements of the array
- Index value of array element ( Subscript )
- The array itself
When the callback function has only one value value The other two parameters can be omitted
for example :
Put the following array value Greater than 18 The number of is returned to a new array
const arr = [1, 6, 7, 3, 4, 5, 102, 99, 78];
const newarr = arr.filter((value) => {
return value > 18;
})
console.log(newarr);
design sketch :

3,reduce() Method
reduce() Method executes user supplied on each element of the array “reducer” Callback function , Pass in the calculated return value from the previous element in sequence , Simple speak Namely Returns the sum of all elements in the array The value of the original group will not be changed Callback functions will not be performed on empty arrays
reduce() There are four parameters
- The first parameter total -- Is the initial value , Return value again Must write
- The second parameter currentValue -- The current element Must write
- The third parameter currentIndex-- Index of the current element Write but not write
- Fourth parameter arr -- The array itself Write but not write
You can also specify an initial value
for example 1
Find an array arr= [1,54,3,3,9,100] The cumulative value of
const arr= [1,54,3,3,9,100];
const newarr = arr.reduce((total,currentValue)=>{
console.log('total Value '+total,'currentValue Value '+currentValue);
return total + currentValue;
})
console.log(newarr);design sketch :

for example 2
When I specify the default value as 30 When ,arr The cumulative value of is ;
const arr= [1,54,3,3,9,100];
const newarr = arr.reduce((total,currentValue)=>{
console.log('total Value '+total,'currentValue Value '+currentValue);
return total + currentValue;
},30)
console.log(newarr);here total The initial value of becomes 30

4,forEach() Method
forEach() Method be used for Call each element of the array , And put the elements Pass to callback function The value of the original group will not be changed Callback functions will not be performed on empty arrays
forEach() The function has three arguments
currentValue : Array current valueindex: The array is currently worth indexingarr: The array itself
for example :
Get array arr All elements of
const arr = [12,54,37,28,10];
const newarr=arr.forEach((item,index,array)=>{
console.log(' Elements in an array '+item);
// console.log(' Index of each array element '+index);
// console.log(' The array itself '+array);
}) design sketch :
Summary : The above is what this article introduces ES6 Several new methods commonly used in , If you want to know more relevant knowledge, you can consult relevant literature .
边栏推荐
- [C language brush leetcode] 676 Implement a magic Dictionary (m)
- MySQL variables, process control and cursors
- The 13th update of go project [ten years of open source] (the "corpse fraud" two months after the change)
- 低代码搭建医疗企业数字化CRM案例分析
- 浅学js中的关系运算符
- 为什么网络安全在工厂中很重要?
- 升级版打榜活动再次火热袭来,放眼望去全是奖金
- [C language brush leetcode] 1432 The maximum difference that can be obtained by changing an integer (m)
- Simple and efficient code suggestions
- Phabricator Conduit API介绍
猜你喜欢

Build an enterprise level data governance system from 0 to 1!

【微信小程序】progress(93/100)

Idea creates new branches and merges code (others - > DEV)

为什么网络安全在工厂中很重要?

Phabricator Conduit API介绍

机器学习笔记 - 使用 GAN 进行数据增强以进行缺陷检测

Technology cloud report: what is the core ability to build observability?

如何应对供应链中第三方的安全风险

华为和荣耀手机升级鸿蒙系统之后与matebook无法多屏协同的问题

Machine learning notes - data enhancement using Gan for defect detection
随机推荐
Current limiting configuration of freeswitch
低代码平台搭建学生事务一门式管理系统解决方案
Use of prettier code formatting tool
不同的图像patch由不同的专家模型来处理!南洋理工&Mila稀疏融合混合专家模型SF-MoE,具有超强泛化能力!代码已开源!...
阿普奇 E8 工控机——MinipiceCAN卡在建筑机器人的应用
华为和荣耀手机升级鸿蒙系统之后与matebook无法多屏协同的问题
MySQL variables, process control and cursors
Phabricator Conduit API介绍
Leetcode+ 86 - 90 double pointer, backtracking, interval DP topics
Introduction to phabricator conduct API
[UI app mobile UI framework]
scrapy 快速下载
Layoffs are coming
[Huang ah code] getting started with MySQL - 2. Using data definition language (DDL) to operate the database
[Huang ah code] Why do I suggest you choose go instead of PHP?
I rolled up a small and beautiful [markdown static blog program] at home
Best practices for the use of exclusive resource pools - Introduction to the use of dependent services
Everything you need to know about cognitive analysis
【图文并茂】U盘启动盘制作 U盘启动盘重装系统教程
FreeSwitch的限流配置