Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Destructing assignment with dots in property [duplicate]

I have an object like this:

const myObject = { 'docs.count': 1000, uuid: 11244, 'pri.store.size': 2453 }

I would like to do a destructuring assignment. Is that only possible for this type of fields?

const { uuid } = myObject;

Thanks!

like image 915
gaheinrichs Avatar asked Sep 20 '25 20:09

gaheinrichs


1 Answers

Variable names can't include a dot, so you can't get do const docs.count = 1000, for example. Destructuring allows you to extract the values even if the property name can't be a the name of a variable, but you'll need to assign them a valid variable name:

const myObject = { 'docs.count': 1000, uuid: 11244, 'pri.store.size': 2453 }

const { 'docs.count': docsCount } = myObject;

console.log(docsCount);
like image 90
Ori Drori Avatar answered Sep 22 '25 10:09

Ori Drori