zoukankan      html  css  js  c++  java
  • js 数组map用法 Array.prototype.map()

    map
    这里的map不是“地图”的意思,而是指“映射”。[].map(); 基本用法跟forEach方法类似:

    array.map(callback,[ thisObject]);
    

    callback的参数也类似:

    [].map(function(value, index, array) {
        // ...
    });
    

    map方法的作用不难理解,“映射”嘛,也就是原数组被“映射”成对应新数组。下面这个例子是数值项求平方:

    var data = [1, 2, 3, 4];
    
    var arrayOfSquares = data.map(function (item) {
      return item * item;
    });
    
    alert(arrayOfSquares); // 1, 4, 9, 16
    

    callback需要有return值,如果没有,就像下面这样:

    var data = [1, 2, 3, 4];
    var arrayOfSquares = data.map(function() {});
    
    arrayOfSquares.forEach(console.log);
    

    结果如下图,可以看到,数组所有项都被映射成了undefined:
    全部项都成了undefined

    在实际使用的时候,我们可以利用map方法方便获得对象数组中的特定属性值们。例如下面这个例子(之后的兼容demo也是该例子):

    var users = [
      {name: "张含韵", "email": "zhang@email.com"},
      {name: "江一燕",   "email": "jiang@email.com"},
      {name: "李小璐",  "email": "li@email.com"}
    ];
    
    var emails = users.map(function (user) { return user.email; });
    
    console.log(emails.join(", ")); // zhang@email.com, jiang@email.com, li@email.com
    

    Array.prototype扩展可以让IE6-IE8浏览器也支持map方法:

    if (typeof Array.prototype.map != "function") {
      Array.prototype.map = function (fn, context) {
        var arr = [];
        if (typeof fn === "function") {
          for (var k = 0, length = this.length; k < length; k++) {      
             arr.push(fn.call(context, this[k], k, this));
          }
        }
        return arr;
      };
    }
    

    附官方Polyfill方法:

    // 实现 ECMA-262, Edition 5, 15.4.4.19
    // 参考: http://es5.github.com/#x15.4.4.19
    if (!Array.prototype.map) {
      Array.prototype.map = function(callback, thisArg) {
    
        var T, A, k;
    
        if (this == null) {
          throw new TypeError(" this is null or not defined");
        }
    
        // 1. 将O赋值为调用map方法的数组.
        var O = Object(this);
    
        // 2.将len赋值为数组O的长度.
        var len = O.length >>> 0;
    
        // 3.如果callback不是函数,则抛出TypeError异常.
        if (Object.prototype.toString.call(callback) != "[object Function]") {
          throw new TypeError(callback + " is not a function");
        }
    
        // 4. 如果参数thisArg有值,则将T赋值为thisArg;否则T为undefined.
        if (thisArg) {
          T = thisArg;
        }
    
        // 5. 创建新数组A,长度为原数组O长度len
        A = new Array(len);
    
        // 6. 将k赋值为0
        k = 0;
    
        // 7. 当 k < len 时,执行循环.
        while(k < len) {
    
          var kValue, mappedValue;
    
          //遍历O,k为原数组索引
          if (k in O) {
    
            //kValue为索引k对应的值.
            kValue = O[ k ];
    
            // 执行callback,this指向T,参数有三个.分别是kValue:值,k:索引,O:原数组.
            mappedValue = callback.call(T, kValue, k, O);
    
            // 返回值添加到新数组A中.
            A[ k ] = mappedValue;
          }
          // k自增1
          k++;
        }
    
        // 8. 返回新数组A
        return A;
      };      
    }
    
  • 相关阅读:
    Ext JS 6应用程序Build后出现“c is not a constructor return new c(a[0])”的处理
    安卓四大组件总览
    摆脱命令行,Ubuntu下配置Android开发环境
    【翻译】使用Sencha Ext JS创建美丽的图画(1)
    linux后台运行springboot项目
    AES地址栏传参加密
    最全的常用正则表达式大全——包括校验数字、字符、一些特殊的需求等等
    阿里云服务器+ftp文件操作+基于Centos7的vsftpd配置
    解决服务器发回了不可路由的地址。使用服务器地址代替的问题
    解决vsftpd的refusing to run with writable root inside chroot错误
  • 原文地址:https://www.cnblogs.com/ysk123/p/9997760.html
Copyright © 2011-2022 走看看