destructing javascript object on return 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: object destructuring
let a, b, rest;
[a, b] = [10, 20];
console.log(a);
// expected output: 10
console.log(b);
// expected output: 20
[a, b, ...rest] = [10, 20, 30, 40, 50];
console.log(rest);
// expected output: Array [30,40,50]