【发布时间】:2019-08-27 01:55:27
【问题描述】:
根据 Mozilla 开发者网站:
The flatMap() method first maps each element using a mapping function, then flattens the result into a new array. It is identical to a map followed by a flat of depth 1, but flatMap is often quite useful, as merging both into one method is slightly more efficient.
例子:
let arr = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10];
const flatMap = arr.flatMap(x => x);
console.log(flatMap);
TypeError: arr.flatMap() is not a function
为什么会返回这个错误?
编辑
我正在通过 Atom 文本编辑器运行它,并使用 HomeBrew 使用 brew upgrade node 将其更新到最新版本,但它仍然给我同样的错误。
我也试过npm install n -g
【问题讨论】:
-
您的浏览器可能还不支持它。但是,在该数组上使用
flatMap有什么意义? -
我只是想看看它是否有效,然后也测试其他值。
-
The documentation 有很多例子,如果你的浏览器不支持alternatives
-
这只是browser compatibility的问题,因为它只支持新的浏览器。
标签: javascript arrays node.js ecmascript-6 flatmap