Skip to content Skip to sidebar Skip to footer

Es6: Destructuring An Object With Symbols As Keys

I have an object that contains symbols as keys. How do I do destructuring assignment in this case? let symbol = Symbol() let obj = {[symbol]: ''} let { /* how do I create a variabl

Solution 1:

Use an alias (see assigning to new variable names):

let symbol = Symbol()
let obj = { [symbol] : 'value'}
let { [symbol]: alias } = obj

console.log(alias)

Solution 2:

Use the same syntax for destructuring as for building the object:

let symbol = Symbol()
let obj = {[symbol]: 'foo'}
let { [symbol]: myValue } = obj;
console.log(myValue);

Post a Comment for "Es6: Destructuring An Object With Symbols As Keys"