complex object destructuring code example

Example 1: how to destructure complex objects

const sampleState = {
 name: “Michael”,
 age: 36,
 location: {
   state:OK,
   city: “Edmond”,
   postal:73012},
 relatives: {
   wife: {
   name: “Shelley”
   }
 }
}

const { age, location: { city, state }, relatives: { wife: { name } } } = sampleState

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);