question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Why use prefer-destructuring on arrays?

See original GitHub issue

Hello 😃

Take this example

const test = {};
const something = [1, 2, 3];

test.value = something[0]; // triggers error

I guess

[test.value] = something;

fixes it, but it is just unreadable.

I am curious why forcing this rule on arrays?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:20
  • Comments:30

github_iconTop GitHub Comments

19reactions
micchickenburgercommented, Jan 7, 2019

Here’s an example why forcing destructuring on arrays of n length is quite silly:

const match = url.match(/^(.+):\/\/(.+):(\d+)\/?/);
// match[0]: result
// match[1]: protocol
// match[2]: hostname
// match[3]: port

// Get port
const port = match[3];

// with destructuring
const [, , , port] = match;  // wth

Of course, this example is quite trivial. Why would I use capturing parenthesis if I wasn’t going to use that value? I could just do this: const [, protocol, hostname, port] = match;

But sometimes you receive an array you didn’t create, and you only need item at index 7, for example. The syntax starts to look a bit ludicrous. I support requiring destructuring arrays of length one, but not more than that.

16reactions
ericbladecommented, Apr 25, 2018

Arrow function syntax and object destructuring are also “unreadable” until you get used to them 😃

I believe ljharb would say that accessing a specific element inside an array is a sign of “code smell”. In general, most of the time you are going to need to do array operations, you are going to be performing them on the entire contents of an array. If you’re using an array that you need to access by individual element, you might be using the wrong data structure.

Read more comments on GitHub >

github_iconTop Results From Across the Web

prefer-destructuring - ESLint - Pluggable JavaScript Linter
With JavaScript ES6, a new syntax was added for creating variables from an array index or object property, called destructuring. This rule enforces...
Read more >
Use array destructuring (prefer-destructuring) error on eslint
eslint prefer-destructuring rule has two properties, array and object , can be used to turn on or off the destructuring requirement for each...
Read more >
prefer-destructuring - Rules - ESLint - GitHub Pages
With JavaScript ES6, a new syntax was added for creating variables from an array index or object property, called destructuring. This rule enforces...
Read more >
Array vs Object Destructuring in JavaScript – What's the ...
Whenever you use array destructuring, JavaScript allows you to separate your variable declarations from their assignments.
Read more >
How To Use Destructuring And The Spread Operator? (One Of ...
When To Use Destructuring/Spread Operator With Arrays · Copying An Array · Converting An Array-Like Object To An Array · Destructuring Function ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found