当前位置:网站首页>js中map和forEach的用法
js中map和forEach的用法
2022-07-02 06:22:00 【秦时明月之安康】
前言
编码过程中一直分不清map和forEach的区别,闲下来,弄几个例子研究一下。
一、map
1.简单数组
const a = [ 5, 7, 10, ]
const mapA = a.map((item, index, arr) => {
console.log(item, index, arr)
return item * 2
})
console.log(mapA)
输出:
2.对象数组
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)
输出:
所以事实上map不仅可以遍历数组,还可以返回一个新数组。
3.修改简单数组内容
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)
输出:
4.修改对象数组内容
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)
输出:
可见,对象数组在遍历的过程中,修改属性的值就可以影响到原数组
map的其他用法详见文档
二、forEach
1.简单数组
const a = [ 5, 7, 10, ]
a.forEach((item, index, arr) => {
arr[index] = item * 2
console.log(item, index, arr)
})
console.log(a)
输出:
2.对象数组
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)
输出:可见,对象数组在遍历的过程中,修改属性的值就可以影响到原数组
3.forEach() 的 continue 与 break
边栏推荐
- 【张三学C语言之】—深入理解数据存储
- selenium备忘录:selenium\webdriver\remote\remote_connection.py:374: ResourceWarning: unclosed<xxxx>解决办法
- 默认google浏览器打不开链接(点击超链接没有反应)
- NodeJs - Express 中间件修改 Header: TypeError [ERR_INVALID_CHAR]: Invalid character in header content
- 分布式事务 :可靠消息最终一致性方案
- 最新CUDA环境配置(Win10 + CUDA 11.6 + VS2019)
- 由於不正常斷電導致的unexpected inconsistency;RUN fsck MANUALLY問題已解决
- Storage space modifier in CUDA
- Dynamic global memory allocation and operation in CUDA
- Android - Kotlin 下使用 Room 遇到 There are multiple good constructors and Room will ... 问题
猜你喜欢
Redis - cluster data distribution algorithm & hash slot
ZZQ的博客目录--更新于20210601
Find the highest value of the current element Z-index of the page
Redis——大Key問題
Redis - hot key issues
由于不正常断电导致的unexpected inconsistency;RUN fsck MANUALLY问题已解决
[literature reading and thought notes 13] unprocessing images for learned raw denoising
ctf-web之练习赛
Shardingsphere JDBC
Hydration failed because the initial UI does not match what was rendered on the server. One of the reasons for the problem
随机推荐
Sublime Text 配置php编译环境
日志 - 7 - 记录一次丢失文件(A4纸)的重大失误
RestTemplate请求时设置请求头,请求参数,请求体。
Distributed transactions: the final consistency scheme of reliable messages
Fe - weex uses a simple encapsulated data loading plug-in as the global loading method
底层机制Mvcc
kali最新更新指南
Win10:添加或者删除开机启动项,在开机启动项中添加在用户自定义的启动文件
Pytest (1) case collection rules
Sentinel rules persist to Nacos
Selenium+msedgedriver+edge browser installation driver pit
MySQL的10大经典错误
ZZQ的博客目录--更新于20210601
CUDA中的线程层次
Is there a really free applet?
Detailed definition of tensorrt data format
构建学习tensorflow
Golang--map扩容机制(含源码)
华为MindSpore开源实习机试题
Warp matrix functions in CUDA