Array.prototype.map() 与 forEach() 的区别是什么?
What is the difference between the map() and the forEach() methods on the Array prototype?
题目类型: 技术面试题
这是一道技术面试题,常见于澳洲IT公司面试中。
难度: medium
分类: JavaScript Arrays
标签: map, forEach, immutability
参考答案摘要
map 会返回 新数组 (不改变原数组),适合做数据转换; forEach 只遍历执行回调,返回值为 undefined ,更适合副作用操作(例如日志、更新外部变量)。
本题提供 STAR 原则详细解答和技术解析,登录匠人学院学习中心即可查看完整答案。