ESLint 警告:使用数组解构

ESLint Warning: Use array destructuring

为什么我会收到 ESLint 警告对下一行使用数组解构

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

您必须让 prefer-destructuring flag on in your config. Destructuring 作为 ES6 的一部分添加。

来自关于规则的文档:

With JavaScript ES6, a new syntax was added for creating variables from an array index or object property, called destructuring. This rule enforces the usage of destructuring instead of accessing a property through a member expression.

规则认为这是不正确的:

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

这是正确的:

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

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

对于您的情况,下面应该可以做到。您仍然以这种方式访问​​第一个元素。

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

PS : 您可以随时关闭标志。