Array.prototype方法
来源:互联网 发布:gamemaker for mac 编辑:程序博客网 时间:2024/05/16 12:51
参考地址:Array.prototype
介绍几种常用的
Array.prototype.indexOfArray.prototype.lastIndexOfArray.prototype.everyArray.prototype.someArray.prototype.forEachArray.prototype.mapArray.prototype.filterArray.prototype.reduce
1、indexOf
indexOf()方法返回在该数组中第一个找到的元素位置,如果它不存在则返回-1
var _indexOf = function(){ const arr = [1,2,3,4,5]; return arr.indexOf(9)!=-1?true:false;}console.log(_indexOf());//false
2、lastindexOf
lastIndexOf() 方法返回在该数组中最后一个找到的元素位置,和 indexof相反。
3、every()
every()可是检测数组中的每一项是否符合条件
var _every = function(){ const arr = [1,2,3,4,5,-1]; const result = arr.every(function(item,index){ return item>0; }); return result;}console.log(_every());//false
4、some()
some()可以检测数组中是否有某一项符合条件
var _some = function(){ const arr = [1,2,3,4,5]; const result = arr.some(function(item,index){ return item >4; }) return result;}console.log(_some());//true
5、 forEach()
forEach为每个元素执行对应的方法
var _foreach = function(){ const arr = [1,2,3,4,5] for(const i of arr.keys()){ console.log(arr[i]); }}_foreach();//1,2,3,4,5
6、 map()
map()对数组的每个元素进行一定操作(映射)后,会返回一个新的数组
var _map = function(){ const arr = [ {first_name:"Colin",last_name:"Toh"}, {first_name:"Addy",last_name:"Osmani"}, {first_name:"Yehuda",last_name:"Katz"} ]; return arr.map(function(item,index){ item.full_name = [item.first_name,item.last_name].join(" "); return item; })}console.log(_map());
forEach 与map的区别
forEach:用来遍历数组中的每一项;这个方法执行是没有返回值的,对原来数组也没有影响;数组中有几项,那么传递进去的匿名回调函数就需要执行几次;每一次执行匿名函数的时候,还给其传递了三个参数值:数组中的当前项item,当前项的索引index,原始数组list;理论上这个方法是没有返回值的,仅仅是遍历数组中的每一项,不对原来数组进行修改;但是我们可以自己通过数组的索引来修改原来的数组;
var _foreackO = function(){ const arr = [1,2,3,4,5]; var res = arr.forEach(function (item,index,input) { input[index] = item*10; }); console.log(res);//undefined return arr;}console.log(_foreackO());//10, 20, 30, 40, 50
map: map的回调函数中支持return返回值;return的是啥,相当于把数组中的这一项变为啥(并不影响原来的数组,只是相当于把原数组克隆一份,把克隆的这一份的数组中的对应项改变了);
var _mapO = function(){ const arr = [1,2,3,4,5]; var res = arr.map(function (item,index,input) { return item*10; }); console.log(res);//10,20,30,40,50 return arr;}console.log(_mapO());//1,2,3,4,5
7、 filter
该filter()方法创建一个新的匹配过滤条件的数组
var _filter = function(){ const arr = [1,2,3,4,5]; let res = arr.filter(function(item){ return item === 1; }) console.log(res);//1 return arr;}console.log(_filter());//1,2,3,4,5
8、 reduce()
reduce()可以实现一个累加器的功能,将数组的每个值(从左到右)将其降低到一个值。 说实话刚开始理解这句话有点难度,它太抽象了。
var _reduce1 = function(){ const arr = [1,2,3,4,5,5]; return arr.reduce(function(prev,next){ console.log("prev:",prev); console.log("next:",next); return prev + " " +next; });}var _reduce2 = function(){ const arr = [1,2,3,4,5,5,1,2]; return arr.reduce(function(prev,next){ console.log("prev:",prev); console.log("next:",next); prev[next] = (prev[next]+1) || 1; return prev; },{});}console.log(_reduce1());//1 2 3 4 5 5console.log(_reduce2());//{1: 2, 2: 2, 3: 1, 4: 1, 5: 2}
阅读全文
0 0
- Array.prototype.slice() 方法
- Array.prototype方法
- 【js】Array.prototype.reduce方法
- Array.prototype.splice方法重写
- Array.prototype.toString方法重写
- Array.prototype.slice.call()方法详解
- Array.prototype方法详解(上)
- Array.prototype.slice.call()方法详解
- ES5 中的Array.prototype.reduce( )方法
- Array.prototype.slice.call()方法详解
- Array.prototype
- NodeList 借用call方法使用Array.prototype.map方法
- Array对象的方法实现(1)----Array.prototype.push和Array.prototype.concat(实现常规参数的功能)
- Array对象的方法实现(2)----Array.prototype.every和Array.prototype.fill(实现常规参数的功能)
- Array对象的方法实现(3)----Array.prototype.filter和Array.prototype.find(实现常规参数的功能)
- Array对象的方法实现(4)----Array.prototype.findIndex和Array.prototype.forEach(实现常规参数的功能)
- js利用prototype调用Array的slice方法
- Array.prototype.slice.call()详解及转换数组的方法
- spring security的原理及教程
- ES6学习笔记(四)
- 无埋点数据收集SDK
- CSDN爬虫(二)——博客列表分页爬虫+数据表设计
- 从入门到不放弃:多浏览器的自动化测试(1)- 本地测试
- Array.prototype方法
- 阿里早期Android加固代码的实现分析
- [日推荐]你不理财, 财就不理你! 这3款小程序教你学会理财
- idea史上最全的快捷键操作手册,值得收藏
- <? extends T>和<? super T>:上下界通配符的界限和使用
- 城堡(搜索题)
- Python----ipython
- Struts2配置详情
- BIGEMAP一键离线地图