1

I'm trying to destructure this object:

{
   name: "Bryan",
   last-name: "Enid"
}

and this is not possible:

const {name, last-name} = req.body

Is there a way to destructure this, without changing the initial object key name?

Bryan Enid
  • 414
  • 3
  • 12

1 Answers1

1

You need to rename the variable, because minus is an operator and not part of a variable name.

BTW, name is a property of Window.name. if this is used you need to rename this value as well.

const { name, 'last-name': lastName } = { name: 'foo', 'last-name': 'bar' };

console.log(lastName);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392