javascript object destructuring variables in array property code example
Example 1: object destructuring into this
const demo = { nextUrl: 'nextUrl', posts: 'posts' };
const target = {};
({ nextUrl: target.nextUrl, posts: target.communityPosts } = demo);
console.log(target);
Example 2: object destructuring example
const hero = {
name: 'Batman',
realName: 'Bruce Wayne',
address: {
city: 'Gotham'
}
};
const { realName, address: { city } } = hero;
city;
Example 3: js object destructuring with defaults
const { dogName = 'snickers' } = { dogName: undefined }
console.log(dogName)
const { dogName = 'snickers' } = { dogName: null }
console.log(dogName)
const { dogName = 'snickers' } = { dogName: false }
console.log(dogName)
const { dogName = 'snickers' } = { dogName: 0 }
console.log(dogName)