当前位置:网站首页>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
边栏推荐
- CUDA中的线程层次
- Redis - big key problem
- Fe - use of weex development weex UI components and configuration use
- CUDA用户对象
- When requesting resttemplate, set the request header, request parameters, and request body.
- Blog directory of zzq -- updated on 20210601
- Sentinel Alibaba open source traffic protection component
- selenium备忘录:selenium\webdriver\remote\remote_connection.py:374: ResourceWarning: unclosed<xxxx>解决办法
- ctf-web之练习赛
- Shardingsphere JDBC
猜你喜欢
ctf三计
Sublime Text 配置php编译环境
Redis——Cluster数据分布算法&哈希槽
Name six schemes to realize delayed messages at one go
FE - 微信小程序 - 蓝牙 BLE 开发调研与使用
ZZQ的博客目录--更新于20210601
Idea announced a new default UI, which is too refreshing (including the application link)
CUDA中的线程层次
qq邮箱接收不到jenkins构建后使用email extension 发送的邮件(timestamp 或 auth.......)
apt命令报证书错误 Certificate verification failed: The certificate is NOT trusted
随机推荐
The intern left a big hole when he ran away and made two online problems, which made me miserable
Fe - use of weex development weex UI components and configuration use
Functions of tensorrt
ctf三计
Eggjs -typeorm treeenity practice
20210306转载如何使TextEdit有背景图片
Sentinel Alibaba open source traffic protection component
web自动化切换窗口时报错“list“ object is not callable
FE - Eggjs 结合 Typeorm 出现连接不了数据库
apt命令报证书错误 Certificate verification failed: The certificate is NOT trusted
Redis - hot key issues
CUDA中的Warp Shuffle
CUDA and Direct3D consistency
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)
Function execution space specifier in CUDA
sprintf_ How to use s
FE - 微信小程序 - 蓝牙 BLE 开发调研与使用
No process runs when querying GPU, but the video memory is occupied
Introduce two automatic code generators to help improve work efficiency
Unexpected inconsistency caused by abnormal power failure; Run fsck manually problem resolved