简体   繁体   English

如何在对象属性上使用数组解构?

[英]How to use array destructuring on object property?

const arr = [1,2];

const respond = {};

respond.obj = arr[0];

The above code giving me eslint error prefer-destructuring . 上面的代码给了我eslint错误preferred prefer-destructuring I understand what's it trying to tell but I'm wondering about my above case is possible? 我知道要说什么,但我想知道我的上述情况是否可能?

Below is what I thought it might be working but apparently no 以下是我认为可能有效的内容,但显然没有

const [respond.a,b] = arr;

Is there any similar approach that gives me something as above? 有没有类似的方法可以给我带来上述好处?

You could try this 你可以试试这个

 const arr = [1,2]; const respond = {}; [respond.obj] = arr; console.log(respond); 

Or you can disable the rule for this line: 或者,您可以禁用此行的规则:

respond.obj = arr[0]; // eslint-disable-line prefer-destructuring

It tells you that you have to destructure the array in the first line. 它告诉您必须在第一行中对数组进行解构。

const [firstNumber, secondNumber] = [1,2];

const respond = {};

respond.obj = firstNumber;

console.log(respond);

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

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