Prefer destructuring - already exisiting variable

Gré*_*EUT 1 javascript destructuring node.js ecmascript-6 eslint

my linter is giving me trouble about destructuring.

在此处输入图片说明


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);
Run Code Online (Sandbox Code Playgroud)

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);
Run Code Online (Sandbox Code Playgroud)

adi*_*iga 9

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);
Run Code Online (Sandbox Code Playgroud)