Javascript - 数组上的 flatMap 方法 - (flatMap 不是函数)

新手上路,请多包涵

根据 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

原文由 mph85 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 747
1 个回答

我在开玩笑地测试时得到了这个,这是因为 flatmap 只是 节点 11 的一部分, 而我正在使用节点 10。

作为解决方法,我在我的 require('core-js/stable'); 中添加了 setupTests.ts

我想也有一些浏览器也没有这个。因此,我还将把该要求行放在我的应用程序导入的某个地方。

原文由 Matthew 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题