Why use prefer-destructuring on arrays?
See original GitHub issueHello 😃
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:
- Created 5 years ago
- Reactions:20
- Comments:30
Top 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 >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
Here’s an example why forcing destructuring on arrays of n length is quite silly:
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.
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.