Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is is possible to create a for-of loop without a variable?

Suppose I have a function like this:

const hasAny = xs => {
  for (const x of xs) {
    return true;
  }

  return false;
};

eslint will complain that x is not used, and indeed it isn't.

Can I write a for-of loop that does not declare any variables?

like image 862
sdgfsdh Avatar asked Oct 05 '18 14:10

sdgfsdh


People also ask

What would happen if you didn’t use a loop?

Without using a loop, we could have achieved that same output by using the following code. Without the loop in place, the code block is repetitive and consists of more lines. If we needed to increment through more numbers we would have needed to write even more lines of code. Let’s go over each expression in the loop to understand them fully.

What is the meaning of I in a loop variable?

the loop variable really has no meaning. Rewriting this code only to more readable. you've hidden that meaning by giving it the meaningless name "i". If [...] introducing bugs. For example, if you use "continue" anywhere in the loop, you will do one retry too much. Jan 10 '08 # 13 locally-translated version of a text string.

Why do we need a loop in a code block?

Without the loop in place, the code block is repetitive and consists of more lines. If we needed to increment through more numbers we would have needed to write even more lines of code. Let’s go over each expression in the loop to understand them fully. Our first expression is the initialization. This is what it looks like.

Is it possible to use a numbers table for looping?

However, you can only use the numbers table in cases where you need to loop over data. If you want to loop over database objects and use dynamic SQL like in the previous paragraph, you'll need to resort to a cursor or WHILE loop (or the system stored procs). Luckily in those cases the data sets are small and there's no real performance impact.


1 Answers

According to ESLint issue #2173, you can set a comment to ignore the unused variable. For that, use the following code:

var some_unused_variable; // eslint-disable-line no-unused-vars

A community member of ESLint also states:

We now have a varsIgnorePattern option available for this use case (best suited for ignoring specific unused variable names or patterns across all your files).

And documentation states:

The varsIgnorePattern option specifies exceptions not to check for usage: variables whose names match a regexp pattern. For example, variables whose names contain ignored or Ignored.

Examples of correct code for the { "varsIgnorePattern": "[iI]gnored" } option:

/*eslint no-unused-vars: ["error", { "varsIgnorePattern": "[iI]gnored" }]*/

var firstVarIgnored = 1;
var secondVar = 2;
console.log(secondVar);
like image 106
E. Zacarias Avatar answered Oct 13 '22 01:10

E. Zacarias