js map的初级使用方法

[].map(function(value, index, array) {
    // ...
});
.map映射 可以对数组进行遍历,使用方法和foreach类似
 
callback(即 函数function)必须要有return,否则报错undefined
 
通过Array.prototype   使map方法兼容ie6-ie8,代码如下
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;
  };
}

 文章乃参考、转载其他博客所得,仅供自己学习作笔记使用!!!

posted @ 2018-12-12 10:14  爱学习的吴小瑞  阅读(200)  评论(0编辑  收藏  举报