5 ms·
To not create a intermediate array I think
by augusto-moura 6y ago
To not create a intermediate array I think
- urvader 6y agoThis works fine: [...paragraph.matchAll(regex)].reduce((q,[kv, key, value]) => { q[key] = (q[key] ? q[key] + `,`: ``) + value return q }, {}) https://pastebin.com/yb5QBCm6 https://pastebin.com/yb5QBCm6
- shrew 6y agoIt 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.
- diegof79 6y agoIn 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.