本文主要和大家分享JS的数组遍历的常用方法实例,本文有三种方法,希望能帮助到大家。
第一种:for循环
for(var i=0 , len= arr.length ; i第二种:forEach
var arr=[12,14,15,17,18]; var res=arr.forEach(function(item,index,input){ input[index]=item*10; }); console.log(res); //undefined console.log(arr); //会对原来的数组产生改变参数说明:item:数组中的当前项
index:当前项的索引
input:原始的数组input
重要说明:没有返回值(res还是无法返回新数组,且原数组也没有改变,因为input值没变)
var arr=[12,14,15,17,18]; var res=arr.forEach(function(item,index,input){ return item*10; }); console.log(res); //undefined console.log(arr); //[12,14,15,17,18]没变其他说明:匿名函数的this指向Windows
如果匿名函数中对数组有修改,会修改到原数组
第三种:map
动态WEB网站中的PHP和MySQL:直观的QuickPro指南第2版下载动态WEB网站中的PHP和MySQL详细反映实际程序的需求,仔细地探讨外部数据的验证(例如信用卡卡号的格式)、用户登录以及如何使用模板建立网页的标准外观。动态WEB网站中的PHP和MySQL的内容不仅仅是这些。书中还提到如何串联JavaScript与PHP让用户操作时更快、更方便。还有正确处理用户输入错误的方法,让网站看起来更专业。另外还引入大量来自PEAR外挂函数库的强大功能,对常用的、强大的包
var arr=[12,14,15,17,18]; var res=arr.map(function(item,index,input){ return item*10; }); console.log(res); //[120,140,150,170,180] console.log(arr); //[12,14,15,17,18]参数说明:item:数组中的当前项
index:当前项的索引
input:原始的数组input
重要说明:有返回值 (要是不给返回值,res就是undefined,但res确实是个数组,只要改变input,原数组就会改变)
var arr=[12,14,15,17,18]; var res=arr.map(function(item,index,input){ input[index]=item*10; }); console.log(res); //[undefined, undefined, undefined, undefined, undefined] console.log(arr); //[120,140,150,170,180]其他说明:匿名函数的this指向Windows
如果匿名函数中对数组有修改,会修改到原数组









