object destructuring example
const hero = {
name: 'Batman',
realName: 'Bruce Wayne',
address: {
city: 'Gotham'
}
};
// Object destructuring:
const { realName, address: { city } } = hero;
city; // => 'Gotham'
object destructuring example
const hero = {
name: 'Batman',
realName: 'Bruce Wayne',
address: {
city: 'Gotham'
}
};
// Object destructuring:
const { realName, address: { city } } = hero;
city; // => 'Gotham'
JavaScript nested destructuring
// Destructuring an object > array > object structure
const returnedObject = {
docsArray: [
{socketRoom: '', routes: []},
{socketRoom: '', routes: []},
{socketRoom: '', routes: []},
]
}
// this will destructure the first and second elements from docsArray, and spread the remainder
const {docsArray: [element0, element1, ...remainder]} = returnedObject
// taking this further with destructing properties from element0
const {docsArray: [{socketRoom0, routes0} = element0, {socketRoom1, routes1} = element1]} = returnedObject
// note the syntax for destructing Objects and Arrays
const {propName} = Object
const [element0, element1] = Array
// then one layer deep where Object[propName] is an Array
const {propName: [element0]} = Object
// then two layers, where element0 is an Object
const {propName: [{propName} = element0]}
// three layers
const {propName: [{propName: [element00]} = element0]}
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us