1

The following works fine:

const o = {one:1,two:2,three:3};
const {one,...others}=o;//one=1, others={two:2,three:3}

But how would I do the following:

var o = {['this-is-one']:1,two:2,three:3};
var {['this-is-one'],...others}=o;

Currently that gives me a SyntaxError: Unexpected token ','

I suspect it would not work because this-is-one would be invalid for a constant name (it only works for property values).

HMR
  • 37,593
  • 24
  • 91
  • 160

1 Answers1

2

You need a renaming of the variable name, because the the given key is not a valid variable name.

var o = { 'this-is-one': 1, two: 2, three: 3 },
    { 'this-is-one': thisIsOne, ...others } = o;

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