8

I thought that I would be able to use the follow syntax but it doesn't work as expected ('renamed' not defined):

const test= ({ var1, var2, renamed = var3 }) => { ...

I see that I can just do the following, but I wondered if there is a more concise way to do it from within the parenthesis. Many thanks.

const test= ({ var1, var2, var3 }) => { ...
const renamed = var3; ...
user1088793
  • 623
  • 2
  • 9
  • 19

2 Answers2

12

Try:

const test = ({ var1, var2, var3: renamed }) => { ...
CD..
  • 72,281
  • 25
  • 154
  • 163
2

Your destructuring syntax is wrong, the key comes first followed by a colon : and then the new variable name:

key: newName = defaultValue

const test= ({ var1, var2, var3: renamed = 2 }) =>  console.log(renamed);

var1 = 0;
var2 = 1;
var3 = 2;
test({ var1, var2, var3 })
test({ var1, var2, var3: undefined })
Fullstack Guy
  • 16,368
  • 3
  • 29
  • 44