Objects and Properties
Object Destructuring
Pull named fields out of an object literal.
Object Destructuring
destructuring.js
const boost = 2;
const { name, points } = { name: "Ada", points: 10 + boost };
const level = points >= 12 ? "high" : "base";
console.log("name=" + name);
console.log("points=" + points);
console.log("level=" + level);
Pull Out Fields
booststarts as2.- The object has
name: "Ada"andpoints: 10 + boost. - Destructuring creates local
nameandpointsvariables. points >= 12is true.- The level becomes
high. | Field | Local variable | Value | | --- | --- | --- | |name|name|Ada| |points|points|12|
object-destructuring
Destructuring binds selected properties to local variable names. The later code uses the new variables directly.
Exercise: destructuring.js
Destructure name and points from an object, then label the level from the points