object destructuring in javascript es6 code example
Example 1: object destructuring
const book = {
title: 'Ego is the Enemy',
author: 'Ryan Holiday',
publisher: {
name: 'Penguin',
type: 'private'
}
};
const {title: bookName = 'Ego', author, name: {publisher: { name }} = book, type: {publisher: { type }} = book } = book;
Example 2: javascript deconstruct object
const objA = {
prop1: 'foo',
prop2: {
prop2a: 'bar',
prop2b: 'baz',
},
};
const { prop1, prop2: { prop2a, prop2b } } = objA;
console.log(prop1);
console.log(prop2a);
console.log(prop2b);
Example 3: destructured object
let renseignement = ['voleur' , '10' , 'spécialité'] ;
let [classe , force, magie] = renseignement ;
console.log(classe) ;
console.log(force) ;
console.log(magie) ;
Example 4: object destructuring
let a, b, rest;
[a, b] = [10, 20];
console.log(a);
console.log(b);
[a, b, ...rest] = [10, 20, 30, 40, 50];
console.log(rest);