1
var x = { "a": 1, "b": 3 }

const { a } = x

console.log(a)

For the above code output is 1 as expected.

var x = { "a.0.0": 1, "b": 3 }

const { a.0.0 } = x

console.log(a)

but when the key has dot character in it, the output is undefined. How can I destructure when the key has dot characters in it?

Syntle
  • 5,168
  • 3
  • 13
  • 34
srinivast6
  • 309
  • 3
  • 8

2 Answers2

1

You need to take the key as string and another variable name for getting a valid variable. (Assigning to new variable names)

var x  = { "a.0.0": 1, b: 3 };

const { 'a.0.0': a } = x;

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

Hi here's the code to solve this :)

var x  = {"a.0.0":1,"b":3}

const {"a.0.0": a1} = x;
//or
const a2 = x["a.0.0"]

console.log(a1, a2)