关于map

  map() 方法返回一个由原数组中的每一个元素调用一个指定方法后的返回值组成的新数组。
这里的map不是“地图”的意思,而是指“映射”。[].map(); 基本用法跟forEach方法相似:web

function square(i){  return i*i; }数组

console.og(["1","2","3"].map(square)); //输出 ["1","4","9"]浏览器

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

array.map(function(currentValue,index,arr), thisValue)

map() 方法按照原始数组元素顺序依次处理元素。学习

注意: map() 不会对空数组进行检测。this

注意: map() 不会改变原始数组。spa

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

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方法:code

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;
  };
}
文章乃参考、转载其余博客所得,仅供本身学习做笔记使用!!!

 参考:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/maporm

相关文章
相关标签/搜索