根据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
发布于 2020-05-31 22:59:25
在用jest进行测试时,我得到了这个结果,这是因为flatmap只是节点11的一部分,而我使用的是节点10。
作为解决办法,我在我的setupTests.ts中添加了require('core-js/stable');。
我想也有一些浏览器也不会有这种情况。因此,我还将在我的应用程序导入中添加需要行。
发布于 2019-04-05 07:57:23
您的浏览器似乎不支持flatMap。下面是支持的浏览器的完整列表:https://caniuse.com/#search=flatMap
如果您真的想使用它,下面是一个将支持ES3:https://www.npmjs.com/package/array.prototype.flatmap的多边形填充
顺便说一句,在多维数组上应用它是很有用的!
发布于 2019-04-05 07:57:34
这意味着您使用的是不支持Array.prototype.flatMap的web浏览器或其他开发环境(目前边缘和IE不支持)。CanIUse表这里.
还请注意,它主要用于多维数组(以避免链接map和flat,从而避免flatMap)。
https://stackoverflow.com/questions/55530690
复制相似问题