zoukankan      html  css  js  c++  java
  • JS中map()与forEach()的用法

    相同点:

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

    2.每次执行匿名函数都支持三个参数,参数分别为item(当前每一项),index(索引值),arr(原数组)

    3.匿名函数中的this都是指向window

    4.只能遍历数组

    不同点:

    map()

    map方法返回一个新的数组,数组中的元素为原始数组调用函数处理后的值

    也就是map()进行处理之后返回一个新的数组

    ⚠️注意:map()方法不会对空数组进行检测

    map方法不会改变原始数组

    var arr = [0,2,4,6,8];
    
    var str = arr.map(function(item,index,arr){
    
    console.log(this);    //Window
    
    console.log(this);
    
    console.log(item);
    
    console.log('原数组arr:',arr);   // 会执行五次
    
    return item/2;
    },this);
    
    console.log(str); //[0,1,2,3,4]

    forEach

    forEach方法用于调用数组的每个元素,将元素传给回调函数

    ⚠️注意: forEach对于空数组是不会调用回调函数的 ,

    没有返回一个新数组&没有返回值

    应用场景:为一些相同的元素,绑定事件处理器!

    不可链式调用 

    var arr = [0,2,4,6,8]
    var sum =0;
    var str = arr.forEach(item,index,arr)
    {
    sum+= item;
    console.log("sum的值为:",sum);
    }

    我们先来看两者之间的相同之处

    var arr = ['a','b','c','d'];
    
    arr.forEach(function(item,index,arr){    //item表示数组中的每一项,index标识当前项的下标,arr表示当前数组
        console.log(item);
        console.log(index);
        console.log(arr);
        console.log(this);
    },123);      //这里的123参数,表示函数中的this指向,可写可不写,如果不写,则this指向window
    
    
    arr.map(function(item,index,arr){   //参数含义同forEach
        console.log(item);
        console.log(index);
        console.log(arr);
        console.log(this);
    },123);

    运行之后,可以看出两者参数没有任何的区别,除此之外两者之间还有一个特性,就是不能停止里面的遍历,除非程序报错,那么两者之间的区别在那里呢???

    在于返回值!!!

    var a = arr.forEach(function(item,index,arr){ 
        return 123
    });
    var b = arr.map(function(item,index,arr){
        return 123
    }); 
    console.log(a);    //undefined
    console.log(b);    //[123,123,123,123]

    我们可以利用map的这个特性做哪些事情呢,比如

    var b = arr.map(function(item,index,arr){
        return item+'a';
    }); 
    
    console.log(b); //["aa", "ba", "ca", "da"]
    // 之前我们的循环是这样的  
    for (var index = 0; index < myArray.length; index++) {  
      console.log(myArray[index]);  
    }  
    // 从ES5开始提供这样的for循环  
    myArray.forEach(function (value) {  
      console.log(value);  
    });  
    // 在ES6我们还可以这样任性  
    // 循环下标或者key(for-in)  
    for (var index in myArray) {    // don't actually do this  
      console.log(myArray[index]);  
    }  
      
    // 循环value(for-of)  
    for (var value of myArray) {  
      console.log(value);  
    }  
      
    // 甚至直接循环key和value,no problem  
    for (var [key, value] of phoneBookMap) {  
      console.log(key + "'s phone number is: " + value);  
    }  
      
    // 或者更者我们这样“优雅”的循环对象(貌似和ES6没有关系)  
    for (var key of Object.keys(someObject)) {  
      console.log(key + ": " + someObject[key]);  
    }  
    // 现场实例,我们这样使用  
    var items = [...];  
    items.forEach((item, i) => {  
          if (item.status == 'new') this.apply(item, i)  
    });  
  • 相关阅读:
    1 let和const
    ECMAScript 6 扫盲
    回溯法
    13. Ajax技术
    12.JSTL标签
    11.EL(表达式语言)
    10.正则表达式(未完成)
    博客园自定义样式
    9.一次简单的Web作业
    8.JavaScript
  • 原文地址:https://www.cnblogs.com/yuer20180726/p/11193927.html
Copyright © 2011-2022 走看看