![](/img/trans.png)
[英]JavaScript flatMap() error: “flatMap mapper function is not callable”
[英]Javascript - flatMap method over array - (flatMap is not a function)
根據 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
我在用 jest 測試時得到了這個,這是因為flatmap
只是節點 11 的一部分,而我使用的是節點 10。
作為一種解決方法,我添加了require('core-js/stable');
在我的setupTests.ts
。
我想也有一些瀏覽器也沒有這個。 因此,我還會將該 require 行放在我的應用程序導入中的某處。
您的瀏覽器似乎不支持flatMap
。 這是支持的瀏覽器的完整列表: https : //caniuse.com/#search=flatMap
如果你真的想使用它,這里你是一個 polyfill,它將支持向下到 ES3: https : //www.npmjs.com/package/array.prototype.flatmap
順便說一句,當應用於多維數組時它很有用!
這意味着您使用的網絡瀏覽器或其他開發環境不支持Array.prototype.flatMap
(目前 Edge 和 IE 不支持)。 CanIUse 表在這里。
另請注意,它主要用於多維數組(以避免鏈接map
和flat
,因此flatMap
)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.