Array方法学习小结
原生js forEach()和map()遍历
A:相同点:
1.都是循环遍历数组中的每一项。
2.forEach() 和 map() 里面每一次执行匿名函数都支持3个参数:数组中的当前项item,当前项的索引index,原始数组input。
3.匿名函数中的this都是指Window。
4.只能遍历数组。
B:不同
1.forEach()
没有返回值
var ary = [12,23,2,3];
var res = ary.forEach(function (item,index,ary) {
input[index] = item*10;
})
console.log(res);//-->undefined;
console.log(ary);//-->会对原来的数组产生改变;
2.map()
有返回值,可以return 出来。
var ary = [12,2,4,22,1];
var res = ary.map(function (item,index,ary) {
return item*10;
})
console.log(res);//[120,20,40,220,10];
console.log(ary);//[12,2,4,22,1];
3.filter()筛选(从这个https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/filter网站上copy的demo)
筛选值
function isBigNum(element) {
return element >= 10;
}
var filtered = [12, 5, 8, 10, 40].filter(isBigNum);
// filtered is [12, 10, 40]
筛选json中的无效值
var arr = [
{ id: 15 },
{ id: -1 },
{ id: 0 },
{ id: 3 },
{ id: 12.2 },
{ },
{ id: null },
{ id: NaN },
{ id: 'undefined' }
]; var invalidEntries = 0; function isNumber(obj) {
return obj !== undefined && typeof(obj) === 'number' && !isNaN(obj);
} function filterByID(item) {
if (isNumber(item.id) && item.id !== 0) {
return true;
}
invalidEntries++;
return false;
} var arrByID = arr.filter(filterByID); console.log('Filtered Array\n', arrByID);
// Filtered Array
// [{ id: 15 }, { id: -1 }, { id: 3 }, { id: 12.2 }] console.log('Number of Invalid Entries = ', invalidEntries);
// Number of Invalid Entries = 5
数组中筛选:
var color = ['red', 'grey', 'green', 'yellow', 'orange']; /**
* Array filters items based on search criteria (query)
*/
function filterItems(query) {
return color.filter(function(el) {
return el.toLowerCase().indexOf(query.toLowerCase()) > -1;
})
} console.log(filterItems('re')); // ['red', 'grey', 'green']
console.log(filterItems('or')); // ['orange']
ES5实现
var color = ['red', 'grey', 'green', 'yellow', 'orange'];
const filterItems = (query) => {
return color.filter((el) =>
el.toLowerCase().indexOf(query.toLowerCase()) > -1
);
}
console.log(filterItems('re')); // ['red', 'grey', 'green']
console.log(filterItems('or')); // ['orange']
目前先整理这么多,等下次有时间接着整理。
Array方法学习小结的更多相关文章
- javascript Array 方法学习
原生对象Array学习 Array.from() 从类似数组的对象或可迭代的对象返回一个数组 参数列表 arraylike 类似数组的对象或者可以迭代的对象 mapfn(可选) 对对象遍历映 ...
- C# 静态成员和方法的学习小结
C# 静态成员和方法的学习小结 数据成员:数据成员可以分静态变量.实例变量两种.静态成员:静态成员变量是和类相关联的,可以作为类中"共"有的变量(是一个共性的表现),他不依赖特定对 ...
- objective-c基础教程——学习小结
objective-c基础教程——学习小结 提纲: 简介 与C语言相比要注意的地方 objective-c高级特性 开发工具介绍(cocoa 工具包的功能,框架,源文件组织:XCode使用介绍) ...
- dubbo学习小结
dubbo学习小结 参考: https://blog.csdn.net/paul_wei2008/article/details/19355681 https://blog.csdn.net/liwe ...
- Python 学习小结
python 学习小结 python 简明教程 1.python 文件 #!/etc/bin/python #coding=utf-8 2.main()函数 if __name__ == '__mai ...
- react学习小结(生命周期- 实例化时期 - 存在期- 销毁时期)
react学习小结 本文是我学习react的阶段性小结,如果看官你是react资深玩家,那么还请就此打住移步他处,如果你想给一些建议和指导,那么还请轻拍~ 目前团队内对react的使用非常普遍,之 ...
- pthread多线程编程的学习小结
pthread多线程编程的学习小结 pthread 同步3种方法: 1 mutex 2 条件变量 3 读写锁:支持多个线程同时读,或者一个线程写 程序员必上的开发者服务平台 —— DevSt ...
- (转) Parameter estimation for text analysis 暨LDA学习小结
Reading Note : Parameter estimation for text analysis 暨LDA学习小结 原文:http://www.xperseverance.net/blogs ...
- python --- 字符编码学习小结(二)
距离上一篇的python --- 字符编码学习小结(一)已经过去2年了,2年的时间里,确实也遇到了各种各样的字符编码问题,也能解决,但是每次都是把所有的方法都试一遍,然后终于正常.这种方法显然是不科学 ...
随机推荐
- python itchat 微信开发
使用itchat可以简单操作微信,进行好友和群消息的发送 安装: pip install itchat 使用: import itchat, time # 登录 itchat.auto_login(h ...
- devexpress chart 散点图加载并分组显示(可以自定义颜色 同组中的点颜色相同)
this.dChart.Diagram.Series.Clear();//清空图的内容 var groups = result.GroupBy(itm => itm["flag&quo ...
- 加密算法IV的作用
使用随机数产生的初始化向量才能达到语义安全(散列函数与消息验证码也有相同要求),并让攻击者难以对同一把密钥的密文进行破解 初始化向量的值依密码算法而不同.最基本的要求是“唯一性”,也就是说同一把密钥不 ...
- Python学习---深浅拷贝的学习
浅拷贝copy(): 修改字符串,原来的不变: 更改列表,2个同时修改,以为列表里面还涉及一个指针的索引. 简单讲就是copy()只是简单地copy了第一层,第二层不被copy 深拷贝: 需要单独的 ...
- 玩转Windows/Linux tftp命令
tftp很好理解, 主要用来传文件, 下面以我的操作来谈谈tftp中最重要的几个命令. 一. Windows上的sftp命令(据说Linux上也是这样, 当然此处是指非嵌入式的Linux) 步骤: a ...
- Python深入学习之《Fluent Python》 Part 1
Python深入学习之<Fluent Python> Part 1 从上个周末开始看这本<流畅的蟒蛇>,技术是慢慢积累的,Python也是慢慢才能写得优雅(pythonic)的 ...
- jmeter中类型转换,字符串,转数字型或浮点型
最近在做接口,使用的是jemter工具,在使用jemter工具中,基础的和高级的,在贺满的博客中可以查看到,https://www.cnblogs.com/puresoul/p/5092628.htm ...
- python 浮点运算 及 小数点精确位数
>>> 1050 / 3133>>> 1050 / float(31)33.87096774193548 # 分子或者分母用 ...
- UVa 1151 - Buy or Build(最小生成树)
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- 牛客网多校训练第三场 C - Shuffle Cards(Splay / rope)
链接: https://www.nowcoder.com/acm/contest/141/C 题意: 给出一个n个元素的序列(1,2,...,n)和m个操作(1≤n,m≤1e5),每个操作给出两个数p ...