彻底搞清 JavaScript forEach & map

背景

JavaScript中,数组的遍历我们肯定都不陌生,最常见的两个便是forEach 和 map。

(当然还有别的譬如for, for in, for of, reduce, filter, every, some, ...)

之所以几天要写这个, 是因为前几天写代码的时候犯了一个低级且愚蠢的错误, 最后搞出了个小bug。

最后找到原因, 生气, 甚至还有点想笑, 今天就写一下这两个方法。

对比和结论

forEach: 针对每一个元素执行提供的函数。

map: 创建一个新的数组,其中每一个元素由调用数组中的每一个元素执行提供的函数得来。

直接说结论吧:

forEach方法不会返回执行结果,而是undefined。

也就是说,forEach会修改原来的数组,而map方法会得到一个新的数组并返回。

下面我们看下具体的例子。

forEach

forEach 方法按升序为数组中含有效值的每一项执行一次callback 函数,那些已删除或者未初始化的项将被跳过(例如在稀疏数组上)。

forEach 接收两个参数: arr.forEach(callback[, thisArg]);

callback 函数会被依次传入三个参数:

  1. 数组当前项的值
  2. 数组当前项的索引
  3. 数组对象本身

比如:

const arr = ['1', '2', '3'];
// callback function takes 3 parameters
// the current value of an array as the first parameter
// the position of the current value in an array as the second parameter
// the original source array as the third parameter
const cb = (str, i, origin) => {
  console.log(`${i}: ${Number(str)} / ${origin}`);
};
arr.forEach(cb);
// 0: 1 / 1,2,3
// 1: 2 / 1,2,3
// 2: 3 / 1,2,3

如果 thisArg 参数有值,则每次 callback 函数被调用的时候,this 都会指向 thisArg 参数上的这个对象。

如果省略了 thisArg 参数, 或者赋值为 null 或 undefined,则 this 指向全局对象。

举个勉强的例子,从每个数组中的元素值中更新一个对象的属性:

function Counter() {
  this.sum = 0;
  this.count = 0;
}

Counter.prototype.add = function(array) {
  array.forEach(function(entry) {
    this.sum += entry;
    this.count++;
  }, this);
  // console.log(this) -> Counter
};

var obj = new Counter();
obj.add([1, 3, 5, 7]);

obj.count; 
// 4 
obj.sum;
// 16

如果使用箭头函数来传入函数参数,thisArg 参数会被忽略,因为箭头函数在词法上绑定了 this 值。

一般情况下, 我们只会用到callback的前两个参数。

map

map 做的事情和 for循环 一样,不同的是, map 会创建一个新数组。

所以, 如果你不打算使用返回的新数组, 却依旧使用map的话, 这是违背map的设计初衷的。

map 函数接收两个参数:

  1. callback
    callback 也有三个参数:

    1. currentValue
    2. index
    3. array
  2. thisArg(可选)

这一点和forEach 是类似的。

具体的例子:

const arr = ['1', '2', '3'];
// callback function takes 3 parameters
// the current value of an array as the first parameter
// the position of the current value in an array as the second parameter
// the original source array as the third parameter
const cb = (str, i, origin) => {
  console.log(`${i}: ${Number(str)} / ${origin}`);
};
arr.map(cb);
// 0: 1 / 1,2,3
// 1: 2 / 1,2,3
// 2: 3 / 1,2,3

callback 方法用例:

arr.map((str) => { console.log(Number(str)); })

map 之后的结果是一个新数组, 和原数组是不相等的:

const arr = [1];
const new_arr = arr.map(d => d);
arr === new_arr; // false

你也可以指定第二个参数thisArg的值:

const obj = { name: 'Jane' };

[1].map(function() {
  console.dir(this); // { name: 'Jane' }
}, obj);

但是如果你使用的是箭头函数, 此时的 this将会是 window:

[1].map(() => {
  console.dir(this);   // window
}, obj);

箭头函数和普通函数的工作机制是不同的,不是本范围,你可以看这篇文章了解具体细节:

https://www.geeksforgeeks.org...

写到这里, 就想起来一个经典的题目。

一个使用技巧案例

["1", "2", "3"].map(parseInt);

这道题我们都见过, 我们期望输出 [1, 2, 3], 而实际结果是 [1, NaN, NaN]。

我们简单来分析下过程, 首先看一下参数传递情况:

// parseInt(string, radix) -> map(parseInt(value, index))

第一次迭代(index is 0): parseInt("1", 0); // 1
第二次迭代(index is 1):  parseInt("2", 1); // NaN
第三次迭代(index is 2): parseInt("3", 2); //NaN

看到这, 解决办法也就呼之欲出了:

function returnInt(element) {
  return parseInt(element, 10);
}

['1', '2', '3'].map(returnInt); // [1, 2, 3]

是不是很容易理解?

回到正题。

forEach 和 map 的区别

看两行代码你就懂了:

[1,2,3].map(d => d + 1); // [2, 3, 4];
[1,2,3].forEach(d => d + 1); // undefined;

Vue作者,尤雨溪大佬,有这么一个形象的比方:

foreach 就是你按顺序一个一个跟他们做点什么,具体做什么,随便:

people.forEach(function (dude) {
  dude.pickUpSoap();
});

map 就是你手里拿一个盒子(一个新的数组),一个一个叫他们把钱包扔进去。结束的时候你获得了一个新的数组,里面是大家的钱包,钱包的顺序和人的顺序一一对应。

var wallets = people.map(function (dude) {
  return dude.wallet;
});

reduce 就是你拿着钱包,一个一个数过去看里面有多少钱啊?每检查一个,你就和前面的总和加一起来。这样结束的时候你就知道大家总共有多少钱了。var totalMoney = wallets.reduce(function (countedMoney, wallet) {
return countedMoney + wallet.money;
}, 0);

十分的贴切。

话题链接:https://www.zhihu.com/questio...

顺便送一段简单的原生实现, 感受下区别:

Array.prototype.map = function (fn) {
    var resultArray = [];
    for (var i = 0,len = this.length; i < len ; i++) {
         resultArray[i] = fn.apply(this,[this[i],i,this]);
    }
    return resultArray;
}

Array.prototype.forEach = function (fn) {
    for (var i = 0,len = this.length; i < len ; i++) {
         fn.apply(this,[this[i],i,this]);
    }
}

Array.prototype.reduce= function (fn) {
    var formerResult = this[0];
    for (var i = 1,len = this.length; i < len ; i++) {
         formerResult = fn.apply(this,[formerResult,this[i],i,this]);
    }
    return formerResult;
}

很简单的实现,仅仅实现功能,没做容错处理和特别严格的上下文处理。

什么时候使用 map 和 forEach

因为这两个的区别主要在于是不是返回了一个值, 所以需要生成新数组的时候, 就用map, 其他的就用forEach.

在 React 中, map 也经常被用来遍历数据生成元素:

const people = [
  { name: 'Josh', whatCanDo: 'painting' },
  { name: 'Lay', whatCanDo: 'security' },
  { name: 'Ralph', whatCanDo: 'cleaning' }
];

function makeWorkers(people) {
  return people.map((person) => {
    const { name, whatCanDo } = person;
    return <li key={name}>My name is {name}, I can do {whatCanDo}</li>
  });
}

<ul> {makeWorkers(people)}</ul>

当你不需要生成新书组的时候,用forEach:

const mySubjectId = ['154', '773', '245'];

function countSubjects(subjects) {
  let count = 0;

  subjects.forEach(subject => {
    if (mySubjectId.includes(subject.id)) {
      count += 1;
    }
  });

  return count;
}

const countNumber = countSubjects([
  { id: '223', teacher: 'Mark' },
  { id: '154', teacher: 'Linda' }
]);

countNumber; // 1

这段代码也可以简写为:

subjects.filter(subject => mySubjectId.includes(subject.id)).length;

结论

说了一大堆, 相信大家肯定对这两个方法都有更清楚的认知了,我们再回顾下结论:

forEach 会修改原来的数组,而map方法会得到一个新的数组并返回。

那谁更快? 其实吧, 我们不用纠结到底那个快,反正,都没有for快。

可读性, 才是我们要考虑的。所以需要生成新数组的时候, 就用map, 否则就用forEach.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值