Home > Back-end >  Default parentheses upon object destructuring
Default parentheses upon object destructuring

Time:07-16

I've recently run into a problem that I can't fix.

I'm writing some code where I have to access data with the help of an input range field. I want to write a getData function which looks like this:

function getData() {
  let currentValue = slider.value;
  let index = currentValue - 1;
  return { pageViews, monthlyCost, leftPercentage } = viewsData[index];
}

this works BUT everytime I save my file visual studio code adds a parenthesis around the destructuring assigment like this:

return ({ pageViews, monthlyCost, leftPercentage } = viewsData[index]);

and after that the code doesn't work

can somebody help me with fixing this behavior in my code editor? thanks (:

CodePudding user response:

I believe you have the prettier extension install in your IDE. To prevent this automatic behaviour for that specific line of code, you can use prettier-ignore and exclude the next line from formatting.

function getData() {
  let currentValue = slider.value;
  let index = currentValue - 1;

  // prettier-ignore
  return { pageViews, monthlyCost, leftPercentage } = viewsData[index];
}

CodePudding user response:

@TJ Crowder, you are right, it also works with parenthesis, apparently the error I made had nothing to do with ...

  • Related