5个数组Array方法: indexOf、filter、forEach、map、reduce使用实例
来源:互联网 发布:mac搜狗输入罗马数字 编辑:程序博客网 时间:2024/06/05 18:33
在ES5中,一共有9个Array方法 : Array.prototype.indexOf Array.prototype.lastIndexOf Array.prototype.every Array.prototype.some Array.prototype.forEach Array.prototype.map Array.prototype.filter Array.prototype.reduce Array.prototype.reduceRight
1. indexOf
indexOf()方法返回在该数组中第一个找到的元素位置,如果它不存在则返回-1。
输入:
var arr = ['apple','orange','pear'];console.log("found:", arr.indexOf("orange") );
输出:
found: 1
2. filter
该filter()方法创建一个新的匹配过滤条件的数组。
输入:
var arr = [ {"name":"apple", "count": 2}, {"name":"orange", "count": 5}, {"name":"pear", "count": 3}, {"name":"orange", "count": 16},];var newArr = arr.filter(function(item){ return item.name=='orange';})console.log("Filter results:",newArr.toString());
输出:
Filter results: [{"name":"orange","count":5},{"name":"orange","count":16}]
3. forEach
forEach为每个元素执行对应的方法。
输入:
var arr = [1,2,3,4,5,6,7,8];arr.forEach(function(item,index){console.log("item:"+item+",index:"+index);});
输出:
item:1,index:0item:2,index:1item:3,index:2item:4,index:3item:5,index:4item:6,index:5item:7,index:6item:8,index:7
4. map()
map()对数组的每个元素进行一定操作(映射)后,会返回一个新的数组,。
输入:
var oldArr = [{first_name:"Colin",last_name:"Toh"},{first_name:"Addy",last_name:"Osmani"},{first_name:"Yehuda",last_name:"Katz"}];function getNewArr(){ return oldArr.map(function(item,index){ item.full_name = [item.first_name,item.last_name].join(" "); return item; }); }console.log(getNewArr());
输出:
[{"first_name":"Colin","last_name":"Toh","full_name":"Colin Toh"},{"first_name":"Addy","last_name":"Osmani","full_name":"Addy Osmani"},{"first_name":"Yehuda","last_name":"Katz","full_name":"Yehuda Katz"}]
5. reduce()
reduce()可以实现一个累加器的功能,将数组的每个值(从左到右)将其降低到一个值。。
输入:
var arr = ["apple","orange","apple","orange","pear","orange"];function getWordCnt(){ return arr.reduce(function(prev,next){console.log("prev:"+JSON.stringify(prev)+",next:"+next) prev[next] = (prev[next] + 1) || 1; return prev; },{});}console.log(JSON.stringify(getWordCnt()));
输出:
prev:{},next:appleprev:{"apple":1},next:orangeprev:{"apple":1,"orange":1},next:appleprev:{"apple":2,"orange":1},next:orangeprev:{"apple":2,"orange":2},next:pearprev:{"apple":2,"orange":2,"pear":1},next:orangeVM401:11 {"apple":2,"orange":3,"pear":1}
0 0
- 5个数组Array方法: indexOf、filter、forEach、map、reduce使用实例
- 5个数组Array方法: indexOf、filter、forEach、map、reduce使用实例
- 5个数组Array方法: indexOf、filter、forEach、map、reduce使用实例
- 5个数组Array方法: indexOf、filter、forEach、map、reduce使用实例
- 5个数组Array方法: indexOf、filter、forEach、map、reduce使用实例
- 5个数组Array方法: indexOf、filter、forEach、map、reduce使用实例
- 5个数组Array方法: indexOf、filter、forEach、map、reduce使用实例
- 5个现在就该使用的数组Array方法: indexOf/filter/forEach/map/reduce详解
- 常用数组Array方法: indexOf、filter、forEach、map、reduce使用实例
- JS Array常用方法indexOf/filter/forEach/map/reduce详解
- JS--Array的常用方法map、reduce、filter、forEach、indexOf
- JS数组filter()、map()、some()、every()、forEach()、lastIndexOf()、indexOf()实例
- array之reduce/map/forEach/filter
- ES5中Array新增加的API接口 forEach map filter some every indexOf lastIndexOf reduce reduceRight
- js数组的遍历方法filter()、map()、some()、every()、forEach()、lastIndexOf()、indexOf()
- JavaScript数组forEach()、map()、reduce()方法
- 一张图看懂JavaScript中数组的迭代方法:forEach、map、filter、reduce、every、some
- javascript中数组的迭代方法对比:forEach()、map()、reduce()、some()、every()、filter()图解
- 23中设计模式之门面模式
- c++ 实现url的转码与解码 urlencode urldecode
- 部门团建—十渡
- android Service 之Bound Service
- publishProgress
- 5个数组Array方法: indexOf、filter、forEach、map、reduce使用实例
- 数组长度的可扩张性写法的例子
- jdbc1
- img标签等比例缩放
- mybatis级联查询
- Android中的Selector的用法
- Parajumpers Herren knight the parajumpers
- 鼠标滑过图片切换函数(简化版)鼠标滑动效果
- 加密解密工具类