Consider the following example:
In the example above,
we stored the key name of the property
we want to destructure in keyName
.
Then we destructured the property
using the bracket notation.
{ [keyName]: twelfthPerson } = person
is same as writing
{ 12: twelfthPerson } = person
.
Note that we renamed
the destructured property
because 12
is not a valid variable name.
Similarly, we can also destructure properties
using expressions,
as shown in the example given below: