Javascript - flatMap method over array - (flatMap is not a function)

后端 未结 3 491
迷失自我
迷失自我 2020-12-18 23:29

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

相关标签:
3条回答
  • 2020-12-19 00:09

    It seems that flatMap is not supported on your browser. Here you are a complete list of supported browsers: https://caniuse.com/#search=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!

    0 讨论(0)
  • 2020-12-19 00:25

    It means you're using a web browser or other development environment that does not support Array.prototype.flatMap (currently Edge and IE have no support). CanIUse table here.

    Also note that it is used primarily for multidimensional arrays (to avoid chaining map and flat, hence flatMap).

    0 讨论(0)
  • 2020-12-19 00:30

    I was getting this when testing with jest, it's because flatmap is only part of node 11 and I was using node 10.

    As a workaround, I added require('core-js/stable'); in my setupTests.ts.

    I presume also there are some browsers that won't have this either. As such I will also put that require line in my application imports somewhere.

    0 讨论(0)
提交回复
热议问题