简体   繁体   English

如果我根据数组特定索引中的对象分配两个变量,如何传递 eslint 的“优先解构”规则?

[英]How do I pass eslint's "prefer-destructuring" rule if I'm assigning two variables based off an object in a specific index of an array?

I have array touchEvents.我有数组 touchEvents。 It looks like:看起来像:

evt: {
    touchEvents: [
       {
         clientX: 3,
         clientY: 4,
       }, {...}, ...
    ]
 }

I wanted to reassign two variables to the two properties specifically in the [0] position of the array.我想将两个变量重新分配给两个属性,特别是在数组的[0]位置。

I did:我做了:

clientX = evt.touchEvents[0].clientX;
clientY = evt.touchEvents[0].clientY;

The eslint linter said: eslint linter 说:

error: Use object destructuring (prefer-destructuring)错误:使用对象解构(首选解构)

So I did:所以我做了:

({ clientX, clientY } = evt.touchEvents[0]);

And I got the error我得到了错误

error: Use array destructuring (prefer-destructuring)错误:使用数组解构(首选解构)

I don't understand how to use "array destructuring" to assign these variables.我不明白如何使用“数组解构”来分配这些变量。 I've read the mdn docs and the eslint documentation on destructuring but I still don't understand what it wants of me, or rather, how to use array destructuring to assign these variables.我已经阅读了有关解构的 mdn 文档eslint 文档,但我仍然不明白它想要我做什么,或者更确切地说,如何使用数组解构来分配这些变量。

How can I use "array destructuring" to pass this eslint rule and assign my variables?如何使用“数组解构”来传递这个 eslint 规则并分配我的变量?

in two lines it would be在两行中它会是

const [ touchEvents ] = evt.touchEvents
const { clientX, clientY } = touchEvents

or one-liners或单线

const { clientX, clientY } = evt.touchEvents[0]

or或者

const [{ clientX, clientY }] = evt.touchEvents

if you feel any rule bothers you, you can always disable with // eslint-disable-next-line <the-rule-name>如果您觉得任何规则困扰您,您可以随时使用// eslint-disable-next-line <the-rule-name>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM