3 ms·
‘matchAll’ looks great but why make it an iterator vs a ‘map’ style callback? Just seems so arcane.
by dclowd9901 8y ago
‘matchAll’ looks great but why make it an iterator vs a ‘map’ style callback? Just seems so arcane.
- masklinn 8y ago> ‘matchAll’ looks great but why make it an iterator vs a ‘map’ style callback? Because that makes it significantly easier to e.g. optionally collect into an array, or to only partially iterate the sequence (e.g. only get the first 3 matches) which is painful to impossible using JS's callbacks. An iterator is simply more flexible.
- thomasfoster96 8y agoFor what it’s worth Array.from() [0] lets you pass a map style callback as its second argument, so Array.from(yourStr.matchAll(yourRegex), yourMapFn) works. [0] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
- deleted 8y ago[deleted]