destructing js 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 assignment
Destructuring assignment is special syntax introduced in ES6,
for neatly assigning values taken directly from an object.
const user = { name: 'John Doe', age: 34 };
const { name, age } = user;
Example 3: 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);
Example 4: javascript destructing
const x = [1, 2, 3, 4, 5]
const [a, b] = x
console.log(a)
console.log(b)
Example 5: mdn destructuring
let a, b;
[a, b] = [1, 2];
console.log(a);
console.log(b);
Example 6: js destructuring explained
const { identifier } = expression;