5

I have a function that returns an array with 2 values. In this instance, I only want to use the first one. However, I cannot figure out the way that is accepted by ESLint to accomplish this.

I've tried the following ways, but there does not seem to be a way to satisfy all the rules.

// Triggers no-unused-vars rule
const [fileName, _] = createFile(objects, fileDirectory);
// Triggers comma-spacing rule
const [fileName,] = createFile(objects, fileDirectory);
// Triggers array-bracket-spacing rule
const [fileName, ] = createFile(objects, fileDirectory);

I would expect at least one of the methods to not trigger an ESLint rule violation, or there to be a different valid way of achieving the same.

3
  • 8
    const [fileName] = createFile(objects, fileDirectory);?
    – jonrsharpe
    May 14, 2019 at 12:34
  • That works. I expected that would return the full array instead of just the first element.
    – b9s
    May 14, 2019 at 12:37
  • 1
    [fileName,] and [fileName, ] both do the same thing but in both cases it's useless - you are taking the first value and skipping the second. You'd do the skip thing only need to use for something like [fileName, , thirdValue] - taking the first and third value skipping the second one.
    – VLAZ
    May 14, 2019 at 12:48

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.