-1

I actually have ESlint errors which don't allow me to do so. I have copy-pasted this this piece of code from the styled-system official website.

enter image description here

So, in the end, what do I get in breakpoints? Is it object or array?

jakhando
  • 113
  • 1
  • 9
  • 2
    or `const [sm, md, lg, xl] = breakpoints;` – Reyno Nov 04 '21 at 08:52
  • I saw this here : https://styled-system.com/responsive-styles – jakhando Nov 04 '21 at 08:53
  • Isn't it correct or it's not about assignment ? – jakhando Nov 04 '21 at 08:53
  • What is the end result you want? Do you actually want non-element properties on your array (which is what your code tries to do)? Freestanding variables? Something else? Why do you think you want to use destructuring? – T.J. Crowder Nov 04 '21 at 08:56
  • 1
    Yes it works, but (most of the time) it is bad practice to asign values to array properties hence ESLint is complaining. The reason this works is because an array is actually just an object – Reyno Nov 04 '21 at 08:57
  • 1
    ESLint doesn't accept adding keys to an array. It's confusing and unnecessary anyway. –  Nov 04 '21 at 08:59
  • *"Here I don't understand why ESlint does not allow this assignment"* What specific error are you getting? Please always copy and paste exact error messages rather than just saying that you're getting an error. – T.J. Crowder Nov 04 '21 at 09:09
  • @ChrisG - What rule prevents it? I don't see one. – T.J. Crowder Nov 04 '21 at 09:16

3 Answers3

1

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
kannajs
  • 99
  • 6
  • 1
    I don't see how this answers the question. (In fact, I don't think the question is answerable at the moment.) The question creates properties on the array; this creates freestanding constants instead. *(not my downvote)* – T.J. Crowder Nov 04 '21 at 08:58
  • I like my arrays organized so when i loop through them or i need a certain item in the array i can do with way cleaner with an object inside it, but i edited it and made one also with the original values. – kannajs Nov 04 '21 at 09:05
  • Understandable but be aware that you now need to type `sm.sm` or `md.md` to get the value. Wouldn't it be easier to rename them all to for example `size` so that you can call `sm.size` and `md.size`? – Reyno Nov 04 '21 at 09:11
  • @Reyno Yes! You are right! I changed the code. – kannajs Nov 04 '21 at 09:14
0
export const breakpoints = [512, 768, 1024, 1280];

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

Tested it in JSFiddle with this code:

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.

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. ;)

Bart Hofland
  • 3,700
  • 1
  • 13
  • 22
  • @Reyno - I suspect the OP is running into ESLint's [`prefer-destructuring`](https://eslint.org/docs/rules/prefer-destructuring) rule, which does indeed complain about the original code but not about the above. ("suspect" because they haven't told us.) The error it gives on the original is "Use array destructuring." This is as close to a good answer to this currently-poor question as we're likely to get. – T.J. Crowder Nov 04 '21 at 09:11
  • 2
    Yes correct, just tested it. ESLint indeed doesn't complain about this solution though the functionality is the same – Reyno Nov 04 '21 at 09:14
-1
[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;
Jan-Philipp Marks
  • 1,419
  • 8
  • 13
  • *"This will not work as you cannot mix deconstructing and assigning."* That is incorrect. That code works just fine. Also note that there's no significant difference between your "try this" version and what you've said doesn't work. Both are doing assignment. (Perhaps you meant to have a `const` or `let` at the beginning.) – T.J. Crowder Nov 04 '21 at 09:10
  • It works, you are right, but it is a very dangerous approach as your breakpoints Array will end up in this: [512, 768, 1024, 1280, sm: 512, md: 768, lg: 1024, xl: 1280]. This is quite a chaos and using this approach in more complex code you will be extremly error prone – Jan-Philipp Marks Nov 04 '21 at 09:53
  • That's an entirely different -- and opinion-based -- thing than what you're saying above. (But re the style point: I've used non-element properties on arrays on many occasions, with no problem at all. I generally don't do it these days because people are confused by it, but the code where I did has never been a problem.) – T.J. Crowder Nov 04 '21 at 09:55