JavaScript中map函数和filter的简单举例
2024-08-25 13:55:33
JavaScript的数组迭代器函数map和filter,可以遍历数组时产生新的数组,和python的map函数很类似
1> filter是满足条件的留下,是对原数组的过滤;
2> map则是对原数组的加工,映射成一一映射的新数组
let arr = [1, 2, 3, 4];
let newArr = arr.map(function(item) { // 使用map方法
return item * 2;
});
console.log(newArr); // [2, 4, 6, 8] let arr = [1, 2, 3, 4];
let newArr = arr.filter(function(item) { // 使用filter方法
if (item % 2 !== 0) {
return item;
}
});
console.log(newArr); // [1, 3];
最新文章
- struts2的action是多例,servlet是单例
- 【转】单调队列优化DP
- 单据UI代码开发
- Codeforces Round #189 (Div. 1) B. Psychos in a Line 单调队列
- Oracle EBS-SQL (SYS-23):用户权限查询.sql
- [TroubleShootin]The backup set holds a backup of a database other than the existing &;#39;xxdb&;#39; database.
- 挺好用的SQLSERVER数据库自动备份工具SQLBackupAndFTP(功能全面)
- Ubuntu14.04下SP_Flash_Tool_exe_Linux无法烧录
- HTML5 进阶系列:canvas 动态图表
- 【Xbox one S】开箱&;开机&;初入坑心得
- linux_软件安装策略和升级策略
- 《javascript设计模式与开发实践》阅读笔记(15)—— 装饰者模式
- mySQL安装与基础配置
- Mysql添加字段.md
- vuex - 学习日记
- nexus 组件下载和上传
- 「国庆训练」Bomb(HDU-5934)
- 【SQL Server数据迁移】把csv文件中的数据导入SQL Server的方法
- Codeforces Round #272 (Div. 1) A. Dreamoon and Sums(数论)
- 关于synchronized关键字
热门文章
- 南昌网络赛 I. Max answer (单调栈 + 线段树)
- Java多线程笔记[未更新完]
- header请求头信息详细介绍
- HTML5创业的另一种可能
- 微服务Kong(四)——添加插件
- redis 集群的密码设置
- R语言矩阵matrix函数
- 决策树遇到sklearn.exceptions.NotFittedError: XXX instance is not fitted yet. Call 'fit' with appropriate arguments before using this method.的解决方案
- python模块学习第 0000 题
- Unexpected exception 'Cannot run program '的解决