简体   繁体   English

ESLint 警告:使用数组解构

[英]ESLint Warning: Use array destructuring

为什么我收到以下行的 ESLint 警告Use array destructuring

tmpObj[item].sample = urls[key][0];

You must be having the prefer-destructuring flag on in your config.您必须在配置中启用了prefer-destructuring标志。 Destructuring was added as a part of ES6. 解构是作为 ES6 的一部分添加的。

From the docs about the rule:从关于规则的文档:

With JavaScript ES6, a new syntax was added for creating variables from an array index or object property, called destructuring.在 JavaScript ES6 中,添加了一种新语法,用于从数组索引或对象属性创建变量,称为解构。 This rule enforces the usage of destructuring instead of accessing a property through a member expression.此规则强制使用解构,而不是通过成员表达式访问属性。

The rule considers this as incorrect:该规则认为这是不正确的:

// With `array` enabled
var foo = array[0];
// With `object` enabled
var foo = object.foo;
var foo = object['foo'];

And this as correct:这是正确的:

// With `array` enabled
var [ foo ] = array;
var foo = array[someIndex];

// With `object` enabled
var { foo } = object;

In your case, the below should do it.在你的情况下,下面应该这样做。 You are still accessing the first element this way.您仍然以这种方式访问​​第一个元素。

[tmpObj[item].sample] = urls[key];

PS : You can always switch off the flag. PS:您可以随时关闭标志。

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

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