Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prefer destructuring - already exisiting variable

my linter is giving me trouble about destructuring.

enter image description here


When I'm trying to destructure, it makes me an error, like in the following snippet :

const data = {
  status: 'example',
};

let status = 'foo';

{
  status,
} = data;

console.log(status);

Is there any ways to use destructuration when the variable already exists?


Using let again :

const data = {
  status: 'example',
};

let status = 'foo';

let {
  status,
} = data;

console.log(status);
like image 676
Orelsanpls Avatar asked Aug 16 '26 06:08

Orelsanpls


1 Answers

Add parenthesis around destructuring

From the documentation: Assignment without declaration

The parentheses ( ... ) around the assignment statement are required when using object literal destructuring assignment without a declaration.

{a, b} = {a: 1, b: 2} is not valid stand-alone syntax, as the {a, b} on the left-hand side is considered a block and not an object literal.

However, ({a, b} = {a: 1, b: 2}) is valid, as is var {a, b} = {a: 1, b: 2}

Your ( ... ) expression needs to be preceded by a semicolon or it may be used to execute a function on the previous line.

const data = {
  status: 'example',
};

let status = 'foo';

({ status } = data);

console.log(status);
like image 165
adiga Avatar answered Aug 18 '26 20:08

adiga



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!