5 ms·
Just use match instead? Why use replace and hack into an array? https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/match h
by urvader 6y ago
Just use match instead? Why use replace and hack into an array?
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/match https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
- augusto-moura 6y agoTo 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.
- chrisrhoden 6y agoThis was a time when there was widespread belief that iterating over an array was best done with a library. I know it's weird, but that's what $.each existed for. for ... of did not exist.
- Stratoscope 6y agoThere was a bit more to $.each than that. Originally, a jQuery object that you got from calls like $(foo) was not an array-like object as it is today. The jQuery object had an internal array of the matching DOM elements, but you were supposed to ignore it and instead use $(foo).each(...) to iterate through the elements, or $(foo).get(n) to access a specific element. I thought it would be more convenient if you could just treat the jQuery object itself as an array, which turned out to be a simple change. So that's why you can now do $(foo)[n]. The .get(n) method was kept for compatibility with very old code. At that point, $(foo).each(...) was not as useful as it had been, but it was also kept for compatibility. And $.each(...) was also kept around, as it was the helper function for $(foo).each(...) and related methods. Another fun fact on the first version of the jQuery code: all of the methods on a jQuery object like $().each, $().html, $().css, etc. were not on a prototype object. Whenever you called $(foo) to create a jQuery object, it ran a loop to copy references to all those methods into the jQuery object. Needless to say, that was a bit slow, and got slower as you added plugins. So my other minor architectural contribution was to use a prototype instead of copying all the methods. We were all learning as we went along in those days! :-)
- MatmaRex 6y ago.match() only returns the result of one match, rather than all the matches in the string. To do that, you'd need to use .matchAll() (which didn't exist in 2008), or write an awkward do…while loop. Using .replace() looks neater.
- deleted 6y ago[deleted]