当前位置:网站首页>What are the traversal methods of arrays in JS
What are the traversal methods of arrays in JS
2022-07-27 05:41:00 【weixin_ forty-six million fifty-one thousand two hundred and si】
1)forEach: It doesn't change the original array , no return value
Three parameters , Traversal value , Index value , The array itself
var arr=[1,2,3,4,5,6]
arr.forEach(item=>{
console.log(item);
// no return value
return item*2//undefined
})
2)map: It doesn't change the original array , There is a return value
Parameters and forEach equally
var arr=[1,2,3,4,5,6]
const arrMap=arr.map(item=>{
console.log(item);
// no return value
return item*2
})
console.log(arrMap);//[2,4,6,8,10,12]
3)filter: Filter array , There is a return value , Returns an array of qualified elements , It doesn't change the original array
Parameters and forEach Agreement
var arr=[1,2,3,4,5,6]
const arrFilter=arr.filter(item=>{
console.log(item);
// no return value
return item<5
})
console.log(arrFilter);//[1,2,3,4]
4)for…of…: Returns the elements of the array , Property value of object , Can't traverse ordinary objects
var arr =[1,2,3,4,5]
for(let value of arr){
console.log(value);// Return key value
}
5)reduce: Receive a function , As an accumulator , Do not change the original array
The function has four arguments , But two parameters are most commonly used , And must write , The first one returns the calculated value or initial value , The second is the current element
var arr=[1,2,3,4]
var arrReduce=arr.reduce((pre,item)=>{
return pre+item
},0)// here 0 Is the initial value of the first parameter
console.log(arrReduce);//10
边栏推荐
- 正则表达式
- Exit login and JSX display
- Native token generation encryption and decryption
- Share a multiple-choice question about the process of program compilation (including a brief discussion on the compilation process, the formation and merging process of symbol tables)
- Permission display - dynamic list on the left
- Gallerycms download, installation and configuration
- [MRCTF2020]PYWebsite 1
- 「PHP基础知识」PHP中的标记
- qsort — c语言中自带的排序函数(附带void*、回调函数知识点
- 用户页面管理
猜你喜欢

graph-node部署

Share a multiple-choice question about the process of program compilation (including a brief discussion on the compilation process, the formation and merging process of symbol tables)

深度优先搜索(dfs)简介

Xiaomi mall project_ register

JS中apply、call、bind的区别
![[MRCTF2020]PYWebsite 1](/img/d4/2d9cd06abd7188add668cde77d3075.png)
[MRCTF2020]PYWebsite 1

Share a multiple-choice question about variables (including global variables, local variables, the scope of variables, and life cycle knowledge points)

路由的配置和前往登录首页进行发送请求

XSS知识点

退出登录与jsx显示
随机推荐
First knowledge of C language -- common data types
Package JWT
Introduction to C language functions
页面的配置
C language makes a small maze
Introduction to C language
正则表达式
Introduction of C language base conversion and original complement inverse code point operation
MD5 password encryption
「PHP基础知识」PHP中的注释
SSTI 模板注入
页面的基本布局
维持登录,路由跳转
Think about the role of some documents
First knowledge of C language -- constants and variables
How to perform implicit conversion in JS
进制的特性
[C language switch branch statement and loop statement]
下载url-loader,用limit指定图片大小后,显示不出图片
Introduction and management of heap memory in C language