zoukankan      html  css  js  c++  java
  • JS中forEach和map的区别

    共同点:

    1、都是循环遍历数组中的每一项。
    
    2、forEach()和map()里面每一次执行匿名函数都支持3个参数:数组中的当前项item,当前项的索引index,原始数组input。
    
    3、匿名函数中的this都是指Window。
    
    4、只能遍历数组。

    不同点:

    1.forEach():

    没有返回值,即返回值为undefined

    arr[].forEach(function(value,index,array){
    
      //do something
    
    })
    • 参数:value数组中的当前项, index当前项的索引, array原始数组;
    • 数组中有几项,那么传递进去的匿名回调函数就需要执行几次;
    • 理论上这个方法是没有返回值的,仅仅是遍历数组中的每一项,不对原来数组进行修改;但是可以自己通过数组的索引来修改原来的数组,或当数组项为对象时修改对象中的值;
    • forEach方法只是操作数据而已,数组里的数据是如何引用的呢? js的数据有基本数据类型和引用数据类型,同时引出堆内存和栈内存的概念。对于基本数据类型:Number、String 、Boolean、Null和Undefined,它们在栈内存中直接存储变量名和值。而Object对象的真实数据存储在堆内存中,它在栈内存中存储的是变量名和堆内存的位置。 而在forEach方法里操作了obj对象,实际操作的是对象本身,而数据只是引用了对象的栈内存地址,所以数组里的数据相应改变。 那么为什么forEach方法不能改变数组里的基本变量呢?因为数组内的基本变量,在栈内存内生成了自己的值,并非引用栈内存的地址。
    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 出来。

    1 arr[].map(function(value,index,array){
    2 
    3   //do something
    4 
    5   return XXX
    6 
    7 })

    参数: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 }   
    17  
    18 /** 
    19 * map遍历数组 
    20 * @param callback [function] 回调函数; 
    21 * @param context [object] 上下文; 
    22 */  
    23 Array.prototype.myMap = function myMap(callback,context){  
    24     context = context || window;  
    25     if('map' in Array.prototye) {  
    26         return this.map(callback,context);  
    27     }  
    28     //IE6-8下自己编写回调函数执行的逻辑  
    29     var newAry = [];  
    30     for(var i = 0,len = this.length; i < len;i++) {  
    31         if(typeof  callback === 'function') {  
    32             var val = callback.call(context,this[i],i,this);  
    33             newAry[newAry.length] = val;  
    34         }  
    35     }  
    36     return newAry;  
    37 }  

    参考博客:https://www.cnblogs.com/momo798/p/11649974.html

    今天你学习了吗!!!
  • 相关阅读:
    DGA域名可以是色情网站域名
    使用cloudflare加速你的网站隐藏你的网站IP
    167. Two Sum II
    leetcode 563. Binary Tree Tilt
    python 多线程
    leetcode 404. Sum of Left Leaves
    leetcode 100. Same Tree
    leetcode 383. Ransom Note
    leetcode 122. Best Time to Buy and Sell Stock II
    天津Uber优步司机奖励政策(12月28日到12月29日)
  • 原文地址:https://www.cnblogs.com/nayek/p/11789154.html
Copyright © 2011-2022 走看看