当前位置:网站首页>js中map和forEach的用法
js中map和forEach的用法
2022-07-02 06:40: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
边栏推荐
- Virtualenv and pipenv installation
- Introduce two automatic code generators to help improve work efficiency
- Self cultivation of programmers - Reflection on job hunting
- js中map和forEach的用法
- Présence d'une panne de courant anormale; Problème de gestion de la fsck d'exécution résolu
- Name six schemes to realize delayed messages at one go
- pytest(1) 用例收集规则
- Kotlin - 验证时间格式是否是 yyyy-MM-dd HH:mm:ss
- TensorRT的功能
- [daily question 1] write a function to judge whether a string is the string after the rotation of another string.
猜你喜欢

Code skills - Controller Parameter annotation @requestparam

代码技巧——Controller参数注解@RequestParam

Summary of advertisement business bug replay

Sentinel rules persist to Nacos

最新CUDA环境配置(Win10 + CUDA 11.6 + VS2019)

CUDA中的线程层次

Redis - hot key issues

Sublime Text 配置php编译环境

AWD学习

Latest CUDA environment configuration (win10 + CUDA 11.6 + vs2019)
随机推荐
apt命令报证书错误 Certificate verification failed: The certificate is NOT trusted
Name six schemes to realize delayed messages at one go
Latest CUDA environment configuration (win10 + CUDA 11.6 + vs2019)
Latex 编译报错 I found no \bibstyle & \bibdata & \citation command
eslint配置代码自动格式化
automation - Jenkins pipline 执行 nodejs 命令时,提示 node: command not found
Cglib agent - Code enhancement test
2020-9-23 use of QT timer qtimer class.
构建学习tensorflow
【每日一题】写一个函数,判断一个字符串是否为另外一个字符串旋转之后的字符串。
The intern left a big hole when he ran away and made two online problems, which made me miserable
Does the assignment of Boolean types such as tag attribute disabled selected checked not take effect?
Nodejs - Express middleware modification header: typeerror [err_invalid_char]: invalid character in header content
Warp matrix functions in CUDA
由于不正常断电导致的unexpected inconsistency;RUN fsck MANUALLY问题已解决
Latex在VSCODE中编译中文,使用中文路径问题解决
Data science [9]: SVD (2)
由於不正常斷電導致的unexpected inconsistency;RUN fsck MANUALLY問題已解决
Blog directory of zzq -- updated on 20210601
FE - Weex 使用简单封装数据加载插件为全局加载方法