destructuring object in object code example
Example 1: 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);
console.log(is_verified);
Example 2: 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 3: object destructuring example
const hero = {
name: 'Batman',
realName: 'Bruce Wayne',
address: {
city: 'Gotham'
}
};
const { realName, address: { city } } = hero;
city;
Example 4: destructure to object
({x: oof.x, y: oof.y} = foo);