Destructure Variables with a different name in Javascript
Destructuring Variables is a very useful feature introduced in ES6, Sometimes you may want to use a different variable name than the key in the object while you are Destructuring.
For example in your object if you have same key names at multiple levels then you must use a different name to assign those two variables.
Let say following is your object and you have id in both root and also at user level
let post = {
id: 'pid'
title: 'Post Title',
content: 'Post content',
user: {
id: 'uid',
name: 'Foo'
}
}
Now you must use a different name for id if you want to destructure both the ids into variables. You can achieve this using : operator as follow
let {id: postId, title, content, user: {id: userId} } = post;