3 ms·
Yeah, thats a common criticism of ES6 selector I see a lot. They are so damn verbose.
by 1GZ0 2y ago
Yeah, thats a common criticism of ES6 selector I see a lot.
They are so damn verbose.
- lioeters 2y agoNot only verbose, querySelectorAll returns a NodeList without normal array methods like map or forEach, so I find myself doing the following pretty much every time I use it: [...document.querySelectorAll('.example')] Typically I see people create a utility function $ (querySelector) and $$ (querySelectorAll with conversion to array). But this is fairly typical of the DOM API, some parts are awkwardly designed.
- destineddrip 2y agoFor what it’s worth, NodeLists do offer a forEach method (along with a few iterator returning methods which in contrast aren’t useful for concise style).
- 1GZ0 2y agoHold your horses now, you can absolutely do: document.querySelectorAll('.element').forEach(i => { yourFunction(i); }); I've not used map a whole lot, but I'm positive that it would work as well.
- lioeters 2y agoOh you're right, there is a forEach method. https://developer.mozilla.org/en-US/docs/Web/API/NodeList/forEach https://developer.mozilla.org/en-US/docs/Web/API/NodeList/fo... entries() forEach() item() keys() values() And maybe values().map() will work too.