当前位置:网站首页>Usage of map and foreach in JS
Usage of map and foreach in JS
2022-07-02 06:40:00 【The health of the bright moon in Qin Dynasty】
List of articles
Preface
The coding process has been confused map and forEach The difference between , spare , Make a few examples to study .
One 、map
1. Simple array
const a = [ 5, 7, 10, ]
const mapA = a.map((item, index, arr) => {
console.log(item, index, arr)
return item * 2
})
console.log(mapA)
Output :
2. An array of objects
const c = [
{
key: 'key1',
value: 'value1',
},
{
key: 'key2',
value: 'value2',
},
]
const mapC = c.map((item, index, arr) => {
console.log(item, index, arr)
let res = this.$deepCopy(item)
res.value += index
return res
})
console.log(c)
console.log(mapC)
Output :
So in fact map Not only can I traverse arrays , You can also return a new array .
3. Modify simple array contents
const a = [ 5, 7, 10, ]
const mapA = a.map((item, index, arr) => {
console.log(item, index, arr)
arr[index] = item * 2
return item * 2
})
console.log(a)
console.log(mapA)
Output :
4. Modify the contents of the object array
const c = [
{
key: 'key1',
value: 'value1',
},
{
key: 'key2',
value: 'value2',
},
]
const mapC = c.map((item, index, arr) => {
console.log(item, index, arr)
let res = item
res.value += index
return res
})
console.log(c)
console.log(mapC)
Output :

so , Object array in the process of traversal , Modifying the value of the attribute can affect the original array
map For other uses of, see file
Two 、forEach
1. Simple array
const a = [ 5, 7, 10, ]
a.forEach((item, index, arr) => {
arr[index] = item * 2
console.log(item, index, arr)
})
console.log(a)
Output :
2. An array of objects
const c = [
{
key: 'key1',
value: 'value1',
},
{
key: 'key2',
value: 'value2',
},
]
c.forEach((item, index, arr) => {
console.log(item, index, arr)
item.value += index
})
console.log(c)
Output :
so , Object array in the process of traversal , Modifying the value of the attribute can affect the original array
3.forEach() Of continue And break
边栏推荐
- 20201002 VS 2019 QT5.14 开发的程序打包
- Data science [viii]: SVD (I)
- CUDA用户对象
- IDEA公布全新默认UI,太清爽了(内含申请链接)
- QQ email cannot receive the email sent by Jenkins using email extension after construction (timestamp or auth...)
- kali最新更新指南
- 底层机制Mvcc
- Latex参考文献引用失败 报错 LaTeX Warning: Citation “*****” on page y undefined on input line *
- Golang -- map capacity expansion mechanism (including source code)
- Présence d'une panne de courant anormale; Problème de gestion de la fsck d'exécution résolu
猜你喜欢

The intern left a big hole when he ran away and made two online problems, which made me miserable

ctf-web之练习赛

CUDA中的Warp Shuffle

unittest.TextTestRunner不生成txt测试报告

TensorRT的数据格式定义详解

20201002 vs 2019 qt5.14 developed program packaging

Win电脑截图黑屏解决办法

Redis - big key problem

CTF web practice competition

Distributed transactions: the final consistency scheme of reliable messages
随机推荐
压力测试修改解决方案
CTF three count
Latex 报错 LaTeX Error: The font size command \normalsize is not defined问题解决
Shardingsphere JDBC
Asynchronous data copy in CUDA
Redis---1. Data structure characteristics and operation
js中map和forEach的用法
js中对于返回Promise对象的语句如何try catch
实现strStr() II
Redis - hot key issues
20210306 reprint how to make TextEdit have background pictures
Eggjs -typeorm treeenity practice
There is no way to drag the win10 desktop icon (you can select it, open it, delete it, create it, etc., but you can't drag it)
Introduce two automatic code generators to help improve work efficiency
记录一次RDS故障排除--RDS容量徒增
CTF web practice competition
CUDA与Direct3D 一致性
Win10:添加或者删除开机启动项,在开机启动项中添加在用户自定义的启动文件
CUDA and Direct3D consistency
默认google浏览器打不开链接(点击超链接没有反应)