4 ms·
Either iterate through the `NodeList` with a `for ... of` loop or a `.forEach` method, or convert it to an array using `Array.from()` or `[...nodeList]`. `Node
by runarberg 6y ago
Either iterate through the `NodeList` with a `for ... of` loop or a `.forEach` method, or convert it to an array using `Array.from()` or `[...nodeList]`.
`NodeList` can be a live list (not `querySelectorAll` though) which has some benefits over arrays.
`NodeList` also implements `Symbol.iterator` so you can use your favorite iterator library if you need to map, filter or reduce it. And with the future pipeline operator, you’ll really don’t see a different between a standard array and a fancy structure like `NodeList`.
- dmitriid 6y agoYou just spent three paragraphs describing how NodeList is different, including a reference to a non-existent operator. Nice.
- hombre_fatal 6y agoWell, you called it a "weird object" that throws errors if you "look at it funny." They tried to demystify it for you in just three sentences. That jQuery lets you willfully cling to the corpse of familiarity instead of spending 30 seconds looking up what NodeList was years ago says more about you than anything about jQuery. And with your end-cap comment about the pipeline operator that they were already, helpfully, pointing out was going to be introduced in the future, you just sound like sour grapes. What gives? Kind of a weird attitude to bring to a forum of craftspeople.
- dmitriid 6y ago> Well, you called it a "weird object" that throws errors if you "look at it funny." They tried to demystify it for you in just three sentences. I never said it was mysterious to me. I described it as concisely as possible. It is an object that has a grand total of two array-like methods (and it took the standards bodies two years to add forEach to it). And it does throw an exception on invalid input. So, to work with it without hassle, guess what, you'll have to recreate that "corpse of familiarity" from jQuery: throw an Array.from at it, and wrap it in a try-catch. And that is true for every single improvement to the DOM API. If you look at all the efforts to get rid of jQuery, you'll see people re-implementing half of it for one simple reason: all the improvements are still stunted, underdesigned, and need quite a lot of additional boilerplate to make it useable in any hut the simplest scenarios. (Notable exception: classList. It weirdly behaves and works the way that doesn't screw up developer experience). > And with your end-cap comment about the pipeline operator that they were already, helpfully, pointing out was going to be introduced in the future, you just sound like sour grapes. What gives? The pipeline operator proposal has been there for three years. Excuse me for not jumping with joy when someone quips that there will be no difference between arrays and NodeLists in some glorious future. I prefer reality.