js destructuring change object value code example

Example 1: javascript object destructuring rename

// Renaming
// ----------------------------------------------
const { bar: foo } = { bar: 123 };
bar // Uncaught ReferenceError: bar is not defined
foo // 123


// Renaming with default value
// ----------------------------------------------
const { bar: foo = 123 } = { potato: 456 };
bar // Uncaught ReferenceError: bar is not defined
foo // 123
potato // Uncaught ReferenceError: potato is not defined

Example 2: object destructuring into this

const demo = { nextUrl: 'nextUrl', posts: 'posts' };

const target = {}; // replace target with this

({ nextUrl: target.nextUrl, posts: target.communityPosts } = demo);

console.log(target);

Example 3: destructure to object

({x: oof.x, y: oof.y} = foo);