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;
Object destructuring
Object Destructuring => // The destructuring assignment syntax is a JavaScript expression that makes it possible to unpack values from arrays, or properties from objects, into distinct variables. // example: const user = { id: 42, is_verified: true }; const {id, is_verified} = user; console.log(id); // 42 console.log(is_verified); // true
Source: developer.mozilla.org
javascript object destructing
const employee = {name: ‘ANE01’, email: ‘Anna@example.com’, phone:’0112–345–6789'}; //with destucturing const {name, email, phone} = employee; //without destucturing const name = employee.name; const email = employee.email; const phone = employee.phone;
Source: blog.bitsrc.io
object destructuring example
const hero = { name: 'Batman', realName: 'Bruce Wayne', address: { city: 'Gotham' } }; // Object destructuring: const { realName, address: { city } } = hero; city; // => 'Gotham'
Source: dmitripavlutin.com
Destructuring in ES6
let {a, b, c} = obj
Source: softhunt.net
javascript object destructuring
// In this syntax: let { property1: variable1, property2: variable2 } = object; // The identifier before the colon (:) is the property of the object and the identifier after the colon is the variable.
Source: www.javascripttutorial.net