destructuring dynamic properties 1

destructuring dynamic properties

let object = {x: 3, y: 6}
let propertyName = 'x'

// the value of the destructured property is assigned to a new variable
let {[propertyName]: value} = object

console.log(value) // 3

Here is what the above code is Doing:
1. The object is destructured and the value of the property named by the variable propertyName is assigned to a new variable named value.
2. The value of the variable propertyName is ‘x’.
3. The value of the property named ‘x’ is 3.
4. The value of the variable value is 3.

Similar Posts