destructuring object behavior 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: destructuring objects
({ a, b } = { a: 10, b: 20 });
console.log(a);
console.log(b);
({a, b, ...rest} = {a: 10, b: 20, c: 30, d: 40});
console.log(a);
console.log(b);
console.log(rest);
Example 3: object destructuring example
const hero = {
name: 'Batman',
realName: 'Bruce Wayne',
address: {
city: 'Gotham'
}
};
const { realName, address: { city } } = hero;
city;