当前位置:网站首页>JS中forEach和map方法有什么区别
JS中forEach和map方法有什么区别
2022-07-27 05:03:00 【weixin_46051260】
相同点:都是遍历数组的方法,都不会更改原数组
区别:map分配内存空间存储新数组,并且返回,forEach不会返回
1)forEach
var arr=[1,2,3,4,5,6]
var result=arr.forEach((item,index,arr)=>{
console.log(item,index,arr);
return item*2
})
console.log(arr);
console.log(result);

2)Map
var arr=[1,2,3,4,5,6]
var result=arr.map((item,index,arr)=>{
console.log(item,index,arr);
return item*2
})
console.log(arr);
console.log(result);

边栏推荐
猜你喜欢

用户登录-以及创建、验证短信验证码

程序环境和预处理(下):#define、#undef、命令行编译、条件编译、文件包含(超全整理,建议收藏!!!

如何将Excel表格中的多列内容合并到一列

Utility gadget: kotlin code snippet

数据库迁移报错解决

JS中apply、call、bind的区别

Flask框架创建项目初期总结

C language string function: strlen, strcpy, strcat

Share a multiple-choice question about the process of program compilation (including a brief discussion on the compilation process, the formation and merging process of symbol tables)

2021 Niuke multi school training camp 5 (question b)
随机推荐
flask项目配置
[C language switch branch statement and loop statement]
SQL(MySql)菜鸟教程知识
分享一道关于程序编译过程的选择题(内含编译过程浅谈,符号表的形成合并过程)
自己动手做一个爬虫项目
Trying to evolve_ My first CSDN blog
C语言中堆内存介绍和管理
Share a multiple-choice question about define (including the replacement rules, program environment and preprocessing related knowledge of define during precompiling)
C语言进制转换以及原补反码位运算介绍
js基础练习题
Mysql速成
Flask对数据库的查询以及关联
Redis cluster
322 coin change of leetcode
流程控制-分支
规格管理,及规格选项管理功能实现
GCC 编译选项
Hi3516dv300 environment setup
C语言函数入门介绍
The concept of cloud native application and 15 characteristics of cloud native application