简体   繁体   English

这里如何使用解构?

[英]How to use destructuring here?

Here I don't understand why ESlint does not allow this assignment, please help.这里我不明白为什么ESlint不允许这个赋值,请帮忙。

I saw this piece of code here : https://styled-system.com/responsive-styles我在这里看到了这段代码: https : //styled-system.com/responsive-styles

export const breakpoints = [512, 768, 1024, 1280]

breakpoints.sm = breakpoints[0]
breakpoints.md = breakpoints[1]
breakpoints.lg = breakpoints[2]
breakpoints.xl = breakpoints[3]

You can desctructure it like this.你可以像这样解构它。

 const breakpoints = [{ id: 1, size: 512 }, {id: 2, size: 768}, { id: 3, size: 1024 }, { id: 4, size: 1280}] const breakpointsOriginal = [512, 768, 1024, 1280] const [sm, md, lg, xl] = breakpoints; // You can use it like sm.size and the sm.size value is = 512 const [small, medium, large, extra_large] = breakpointsOriginal; // You can use small and the small value = 512

export const breakpoints = [512, 768, 1024, 1280];

[breakpoints.sm, breakpoints.md, breakpoints.lg, breakpoints.xl] = breakpoints;

Tested it in JSFiddle with this code:使用以下代码在JSFiddle对其进行了测试:

const breakpoints = [512, 768, 1024, 1280];

[breakpoints.sm, breakpoints.md, breakpoints.lg, breakpoints.xl] = breakpoints;

console.log(breakpoints.sm);
console.log(breakpoints.md);
console.log(breakpoints.lg);
console.log(breakpoints.xl);

Worked fine for me.对我来说效果很好。 Note that I needed to remove the export in JSFiddle and I also needed to add semicolons for it to work properly.请注意,我需要删除 JSFiddle 中的export ,还需要添加分号才能正常工作。

Edit:编辑:

I cannot judge your actual scenario, but I would personally try to avoid extending an array object with such additional properties, unless I really need to access those breakpoints both by property name and by array index, depending on the scenario.我无法判断您的实际场景,但我个人会尽量避免使用此类附加属性扩展数组对象,除非我确实需要根据场景通过属性名称和数组索引访问这些断点。

Since I normally only want a single strategy (either array indices or object properties), I would create a "regular" object and add four properties to it.由于我通常只需要一个策略(数组索引或对象属性),因此我会创建一个“常规”对象并向其添加四个属性。

I could initialize those properties with array destructuring:我可以使用数组解构来初始化这些属性:

const bpArr = [512, 768, 1024, 1280];
const breakpoints = {};

[breakpoints.sm, breakpoints.md, breakpoints.lg, breakpoints.xl] = bpArr;

But if there would not be a source array with the numeric values of the breakpoints already, I would simply use an object literal for the breakpoints, which is a lot simpler and easier to read:但是,如果已经没有包含断点数值的源数组,我将简单地为断点使用对象字面量,这更简单易读:

const breakpoints = {
  sm: 512,
  md: 768,
  lg: 1024,
  xl: 1280
};

Ultimately it's up to you to keep the code as clear and simple as possible and make it only as complex as necessary.最终由您来保持代码尽可能清晰和简单,并仅在必要时使其复杂。 ;) ;)

[breakpoints.sm, breakpoints.md, breakpoints.lg, breakpoints.xl] = breakpoints;

This will not work as you cannot mix deconstructing and assigning.这将不起作用,因为您不能混合解构和分配。

Try this:尝试这个:

[sm, md, lg, xl] = breakpoints;

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

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