Object Destructuring Javascript ES6

Alhiane Lahcen - Jan 14 '20 - - Dev Community

// Example 1
// bind variables to different "car1" object properties

const car1 = {
  name: "fiat",
  model: 500,
  weight: 850,
  color: "red"
};


const { name, color, weight } = car1;

Enter fullscreen mode Exit fullscreen mode

// Example 2
// destruct an object property from a variable
// Rename a variable
// set a value to a variable

const car2 = {
  brand: "fiat",
  model: 500,
  weight: 850,
  colors: {
    red: true,
    green: false
  }
};
Enter fullscreen mode Exit fullscreen mode

// Use ":" sign" to change the name of the variable
// Use the "=" sign to assign a value to a variable

const {
  colors: { red: redColor, white: whiteColor = false, brown = "true" }
} = car2;
Enter fullscreen mode Exit fullscreen mode
. . . . . . . . . . . . .
Terabox Video Player