top | item 20566712

(no title)

Ravengenocide | 6 years ago

According to MDN `arr.flatMap()` is indeed equivalent to `arr.map().flat()` (without the Infinity). [1] Testing in the Chrome Devtools it also seems to be the case:

  x = [[[1, 2]], [[2, 3]], [[3, 4]]]
  x.flatMap(x=>x)
  output: [[1,2], [2,3], [3,4]]
  x.map(x=>x).flat()
  output: [[1,2], [2,3], [3,4]]
  x.map(x=>x).flat(Infinity)
  output: [1, 2, 2, 3, 3, 4]
[1]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe... > 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

discuss

order

maxwellg|6 years ago

Is there any reason it isn't called mapFlat? FlatMap indicates to me that there's a flattening, then a mapping, not the other way around.

dmitriid|6 years ago

Ah, I missed that. I though flatMap had depth of Infinity.