DEV Community

Marko V
Marko V

Posted on β€’ Edited on

2

no-loop-func

Continuing the eslint path, this was also a query I got earlier today.

The problem...

function myFunc(complexObject) {
    // Checking validity of complexObject
    // checking in a nested property
    for(var i=0;i<complexObject.subArray.length;i++) {
        var item = complexObject.subArray[i];
        var subItemToWorkWith = item.lines.filter(function(line) {
            return line.header_id === complexObject.header.id;
        });
    }
}
Enter fullscreen mode Exit fullscreen mode

ESlint will complain about filter having a function definition inside a for-loop which is dependent on a variable outside of the scope of the loop itself. To solve this you have to pass a pre-defined function as a callback-parameter to filter-function.

For this particular example it would be to define a function that takes a parameter for the header id of the complexobject, and then return the function that does the actual filtering.

function myFuncHeaderFilter(headerid) {
    return function(line) {
        return line.header_id === headerid;
    };
}
function myFunc(complexObject) {
    // Checking validity of complexObject
    // checking in a nested property
    for(var i=0;i<complexObject.subArray.length;i++) {
        var item = complexObject.subArray[i];
        var subItemToWorkWith = item.lines.filter(
            myFuncHeaderFilter(complexObject.header.id)
        );
    }
}
Enter fullscreen mode Exit fullscreen mode

Again, this makes the code cleaner, easier to read, as long as you group these things in the file or sort them out in a logical structure. It also increases the possibility of code-reuse as well as making it more testable and therefore maintainable.

References that mention the same thing;
https://github.com/eslint/eslint/issues/5044
http://linterrors.com/js/dont-make-functions-within-a-loop

Quadratic AI

Quadratic AI – The Spreadsheet with AI, Code, and Connections

  • AI-Powered Insights: Ask questions in plain English and get instant visualizations
  • Multi-Language Support: Seamlessly switch between Python, SQL, and JavaScript in one workspace
  • Zero Setup Required: Connect to databases or drag-and-drop files straight from your browser
  • Live Collaboration: Work together in real-time, no matter where your team is located
  • Beyond Formulas: Tackle complex analysis that traditional spreadsheets can't handle

Get started for free.

Watch The Demo πŸ“Šβœ¨

Top comments (1)

Collapse
 
codingedgar profile image
codingedgar β€’

Thanks! Didn't understand why the rule, finally understood how to refactor properly πŸ‘

Image of DataStax

AI Agents Made Easy with Langflow

Connect models, vector stores, memory and other AI building blocks with the click of a button to build and deploy AI-powered agents.

Get started for free

πŸ‘‹ Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay