Hacker News new | ask | show | jobs
by urvader 2082 days ago
This works fine:

[...paragraph.matchAll(regex)].reduce((q,[kv, key, value]) => { q[key] = (q[key] ? q[key] + `,`: ``) + value return q }, {})

https://pastebin.com/yb5QBCm6

2 comments

In this case you can use for..of. The Array [... ] restructuring is converting the iterator to array so you can use reduce... but then you are using reduce in an imperative way. With for...of you skip the intermediate array and is more readable.

For...of has a bad rep because eslint usually is configured to show a warning, because the Babel transpile creates less optimal code if it targets old browsers; but is better here.

It does, but it still creates an intermediate array which is what the parent comment was suggesting the use of `replace` worked around.

With that said, there could easily be an array being iterated under the hood with the `replace` method anyway.