问题描述
根据Mozilla开发人员网站:
According to the Mozilla Developer Website:
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
将其更新为最新版本,但它仍然给我同样的错误.
I am running this through Atom text editor and have used HomeBrew to update it to the latest version using brew upgrade node
and it is still giving me the same error.
我也尝试过npm install n -g
推荐答案
您的浏览器似乎不支持flatMap
.这里是支持的浏览器的完整列表: https://caniuse.com/#search=flatMap
It seems that flatMap
is not supported on your browser.Here you are a complete list of supported browsers: https://caniuse.com/#search=flatMap
如果您真的想使用它,那么这里是一个polyfill,它将向ES3提供支持: https://www.npmjs.com/package/array.prototype.flatmap
If you really want to use it, here you are a polyfill that will grant support down to ES3: https://www.npmjs.com/package/array.prototype.flatmap
顺便说一句,将其应用于多维数组时会很有用!
By the way, it is useful when applied on a multidimensional array!
这篇关于Javascript-数组上的flatMap方法-(flatMap不是函数)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!