no-for-loop should detect and ignore variables populated with DOM node if possible
See original GitHub issueFor loops for non-iterable DOM nodes are treated as iterable arrays.
I’m not sure if this is detectable, surely at least some edge cases wouldn’t be - but when the variable is populated immediately above the loop it seems at least theoretically feasible.
Affected rule: no-for-loop
Input:
const visibleItems = document.querySelector('.visible');
for (let x = 0; x < visibleItems.length; x++) {
someFunc(visibleItems[x])
}
Output:
const visibleItems = document.querySelector('.visible');
for (const [x, visibleItem] of visibleItems.entries()) {
someFunc(visibleItem)
}
As an aside, the fact that the auto-fix depluralizes visibleItems[x]
to visibleItem
is a very nice touch!
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
5. Working with Arrays and Loops - JavaScript Cookbook [Book]
One you’ve created an array, using Array object or literal notation, you can access the array elements in a loop, or use any...
Read more >Master the art of looping in JavaScript with these incredible tricks
In Do While loop the condition to be checked is given at the end, and so the loop executes at least once even...
Read more >Traversing an HTML table with JavaScript and DOM Interfaces
This article is an overview of some powerful, fundamental DOM level 1 methods and how to use them from JavaScript. You will learn...
Read more >The 10 Most Common JavaScript Issues Developers Face
If you need help figuring out why your JavaScript isn't working, consult this list of the 10 most common JavaScript issues from a...
Read more >How do I iterate through table rows and cells in JavaScript?
Furthermore, it gives your code access to both element and index vars rather than just the element, and if you prefer to hide...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Your code is invalid.
querySelector
is not iterable because it returns a single element. I think you meant to usequerySelectorAll
which is actually iterable.In short,
no-for-loop
breaks on array-like objects.