当前位置:网站首页>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
边栏推荐
- 代码技巧——Controller参数注解@RequestParam
- apt命令报证书错误 Certificate verification failed: The certificate is NOT trusted
- pytest(1) 用例收集规则
- Codeforces Round #797 (Div. 3) A—E
- Redis---1. Data structure characteristics and operation
- Storage space modifier in CUDA
- ctf三计
- 重载全局和成员new/delete
- unittest.TextTestRunner不生成txt测试报告
- Fe - use of weex development weex UI components and configuration use
猜你喜欢
Distributed transactions: the final consistency scheme of reliable messages
[literature reading and thought notes 13] unprocessing images for learned raw denoising
FE - 微信小程序 - 蓝牙 BLE 开发调研与使用
The intern left a big hole when he ran away and made two online problems, which made me miserable
js中正则表达式的使用
默认google浏览器打不开链接(点击超链接没有反应)
Apt command reports certificate error certificate verification failed: the certificate is not trusted
Detailed definition of tensorrt data format
Sublime Text 配置php编译环境
由於不正常斷電導致的unexpected inconsistency;RUN fsck MANUALLY問題已解决
随机推荐
pytest(1) 用例收集规则
ZZQ的博客目录--更新于20210601
默认google浏览器打不开链接(点击超链接没有反应)
ModuleNotFoundError: No module named ‘jieba.analyse‘; ‘jieba‘ is not a package
Thread hierarchy in CUDA
构建学习tensorflow
分布式事务 :可靠消息最终一致性方案
Linux MySQL 5.6.51 Community Generic 安装教程
Latex 编译报错 I found no \bibstyle & \bibdata & \citation command
Kotlin - 验证时间格式是否是 yyyy-MM-dd HH:mm:ss
FE - Weex 使用简单封装数据加载插件为全局加载方法
[self cultivation of programmers] - Reflection on job hunting Part II
[daily question 1] write a function to judge whether a string is the string after the rotation of another string.
Pytest (2) mark function
阿里云MFA绑定Chrome浏览器
CUDA中的异步数据拷贝
CUDA中的存储空间修饰符
JS modification element attribute flipping commonly used in selenium's Web Automation
VSCODE 安装LATEX环境,参数配置,常见问题解决
Tensorrt command line program