一、原生JS forEach()和map()遍历

共同点:

1.都是循环遍历数组中的每一项。

2.forEach() 和 map() 里面每一次执行匿名函数都支持3个参数:数组中的当前项item,当前项的索引index,原始数组input。

3.匿名函数中的this都是指Window。

4.只能遍历数组。

1.forEach()

没有返回值。

arr[].forEach(function(value,index,array){

  //do something

})

  • 参数:value数组中的当前项, index当前项的索引, array原始数组;
  • 数组中有几项,那么传递进去的匿名回调函数就需要执行几次;
  • 理论上这个方法是没有返回值的,仅仅是遍历数组中的每一项,不对原来数组进行修改;但是可以自己通过数组的索引来修改原来的数组;
  1. var ary = [12,23,24,42,1];
  2. var res = ary.forEach(function (item,index,input) {
  3. input[index] = item*10;
  4. })
  5. console.log(res);//--> undefined;
  6. console.log(ary);//--> 通过数组索引改变了原数组;

2.map()

有返回值,可以return 出来。

arr[].map(function(value,index,array){

  //do something

  return XXX

})

  • 参数:value数组中的当前项,index当前项的索引,array原始数组;
  • 区别:map的回调函数中支持return返回值;return的是啥,相当于把数组中的这一项变为啥(并不影响原来的数组,只是相当于把原数组克隆一份,把克隆的这一份的数组中的对应项改变了);
  1. var ary = [12,23,24,42,1];
  2. var res = ary.map(function (item,index,input) {
  3. return item*10;
  4. })
  5. console.log(res);//-->[120,230,240,420,10];  原数组拷贝了一份,并进行了修改
  6. console.log(ary);//-->[12,23,24,42,1];  原数组并未发生变化

兼容写法:

不管是forEach还是map在IE6-8下都不兼容(不兼容的情况下在Array.prototype上没有这两个方法),那么需要我们自己封装一个都兼容的方法,代码如下:

  1. /**
  2. * forEach遍历数组
  3. * @param callback [function] 回调函数;
  4. * @param context [object] 上下文;
  5. */
  6. Array.prototype.myForEach = function myForEach(callback,context){
  7. context = context || window;
  8. if('forEach' in Array.prototye) {
  9. this.forEach(callback,context);
  10. return;
  11. }
  12. //IE6-8下自己编写回调函数执行的逻辑
  13. for(var i = 0,len = this.length; i < len;i++) {
  14. callback && callback.call(context,this[i],i,this);
  15. }
  16. }
  1. /**
  2. * map遍历数组
  3. * @param callback [function] 回调函数;
  4. * @param context [object] 上下文;
  5. */
  6. Array.prototype.myMap = function myMap(callback,context){
  7. context = context || window;
  8. if('map' in Array.prototye) {
  9. return this.map(callback,context);
  10. }
  11. //IE6-8下自己编写回调函数执行的逻辑
  12. var newAry = [];
  13. for(var i = 0,len = this.length; i < len;i++) {
  14. if(typeof  callback === 'function') {
  15. var val = callback.call(context,this[i],i,this);
  16. newAry[newAry.length] = val;
  17. }
  18. }
  19. return newAry;
  20. }

二、jQuery $.each()和$.map()遍历

共同点:

即可遍历数组,又可遍历对象。

1.$.each()

没有返回值。$.each()里面的匿名函数支持2个参数:当前项的索引i,数组中的当前项v。如果遍历的是对象,k 是键,v 是值。

$.each(arr, function(index,value){

  //do something

})

  • 参数:arr要遍历的数组,index当前项的索引,value数组中的当前项
  • 第1个和第2个参数正好和以上两个函数是相反的,注意不要记错了
  1. $.each( ["a","b","c"], function(i, v){
  2. alert( i + ": " + v );
  3. });
  1. $("span").each(function(i, v){
  2. alert( i + ": " + v );
  3. });

  1. $.each( { name: "John", lang: "JS" }, function(k, v){
  2. alert( "Name: " + k + ", Value: " + v );
  3. });

2.$.map()

有返回值,可以return 出来。$.map()里面的匿名函数支持2个参数和$.each()里的参数位置相反:数组中的当前项v,当前项的索引 i。如果遍历的是对象,k 是键,v 是值。如果是$("span").map()形式,参数顺序和$.each()  $("span").each()一样。

$.map(arr, function(value, index){

  //do something

  return XXX

})

  1. var arr=$.map( [0,1,2], function(v){
  2. return v + 4;
  3. });
  4. console.log(arr);
  1. $.map({"name":"Jim","age":17},function(k, v){
  2. console.log( k+":"+v );
  3. });

链接:

http://www.cnblogs.com/lpy001/p/6196820.html

http://blog.csdn.net/huangpb123/article/details/52756303

原生JS forEach()和map()遍历的区别以及兼容写法的更多相关文章

  1. 应该用forEach改变数组的值吗? 原生JS forEach()和map()遍历的异同点

    应该用forEach改变数组的值吗? https://segmentfault.com/q/1010000013170900?utm_source=index-hottest 由于js中的数组是引用类 ...

  2. 原生JS forEach()和map()遍历,jQuery$.each()和$.map()遍历

    一.原生JS forEach()和map()遍历 共同点: 1.都是循环遍历数组中的每一项. 2.forEach() 和 map() 里面每一次执行匿名函数都支持3个参数:数组中的当前项item,当前 ...

  3. JS的forEach和map方法的区别,还有一个$.each

    forEach()和map()两个方法都是ECMA5中Array引进的新方法,主要作用是对数组的每个元素执行一次提供的函数,但是它们之间还是有区别的.jQuery也有一个方法$.each(),长得和f ...

  4. JS的forEach和map方法的区别

    一.前言 forEach()和map()两个方法都是ECMA5中Array引进的新方法,主要作用是对数组的每个元素执行一次提供的函数,但是它们之间还是有区别的.jQuery也有一个方法$.each() ...

  5. js foreach、map函数

    语法:forEach和map都支持2个参数:一个是回调函数(item,index,input)和上下文: •forEach:用来遍历数组中的每一项:这个方法执行是没有返回值的,对原来数组也没有影响: ...

  6. for ,foreach ,map 循环的区别

    一.for循环 1.for - 循环代码块一定的次数 遍历数组最常用到的for循环,是最为熟知的一种方法 for (var i=0; i<5; i++) { x=x + "The nu ...

  7. forEach() 和 map() 遍历

    1.forEach()   没有返回值. arr[].forEach(function(value,index,array){ //do something }) 参数:value数组中的当前项, i ...

  8. JS forEach()与map() 用法(转载)

    JavaScript中的数组遍历forEach()与map()方法以及兼容写法   原理: 高级浏览器支持forEach方法语法:forEach和map都支持2个参数:一个是回调函数(item,ind ...

  9. 原生JS和jQuery操作DOM的区别小结

    一.Js原生对象和jQuery实例对象的相互转化: (1).原生JS对象转JQ对象: $(DOM对象); (2). JQ对象转原生JS对象: $(DOM对象).get(index); //注意区分eq ...

随机推荐

  1. 我和python的初相识

    认识Python是大二的选修 单纯只是想赚学分而已 后来觉得越来越有趣. 一.python简介 简单来说Python 是一个高层次的结合了解释性.编译性.互动性和面向对象的脚本语言.Python 的设 ...

  2. Java_Object

    说一下java中的Object类. 1.Object: Object是java类库中的一个特殊类,也是所有类的父类. Object类定义了一些有用的方法,由于是根类,这些方法在其他类中都存在,一般是进 ...

  3. [LeetCode] Longest Mountain in Array 数组中最长的山

    Let's call any (contiguous) subarray B (of A) a mountain if the following properties hold: B.length ...

  4. Scala语言笔记 - 第一篇

    目录 Scala语言笔记 - 第一篇 1 基本类型和循环的使用 2 String相关 3 模式匹配相关 4 class相关 5 函数调用相关 Scala语言笔记 - 第一篇 ​ 最近研究了下scala ...

  5. 实战深度学习OpenCV(三):视频实时canny边缘检测

    #include <stdio.h> #include"opencv2/opencv.hpp" using namespace cv; int main() { Vid ...

  6. Spring Cloud,Dubbo及HSF对比

    Round 1:背景 Dubbo,是阿里巴巴服务化治理的核心框架,并被广泛应用于阿里巴巴集团的各成员站点.阿里巴巴近几年对开源社区的贡献不论在国内还是国外都是引人注目的,比如:JStorm捐赠给Apa ...

  7. SQL 常用语法记录

    SQL语法 注意:SQL 对大小写不敏感 可以把 SQL 分为两个部分:数据操作语言 (DML) 和 数据定义语言 (DDL). 数据操作语言 (DML) SQL (结构化查询语言)是用于执行查询的语 ...

  8. [Swift]LeetCode223. 矩形面积 | Rectangle Area

    Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defined b ...

  9. [Swift]LeetCode291. 单词模式 II $ Word Pattern II

    Given a pattern and a string str, find if strfollows the same pattern. Here follow means a full matc ...

  10. [Swift]LeetCode669. 修剪二叉搜索树 | Trim a Binary Search Tree

    Given a binary search tree and the lowest and highest boundaries as L and R, trim the tree so that a ...