const demo = { nextUrl: 'nextUrl', posts: 'posts' };
const target = {}; // replace target with this
({ nextUrl: target.nextUrl, posts: target.communityPosts } = demo);
console.log(target);
function printBasicInfo({firstName, secondName, profession}) {
console.log(firstName + ' ' + secondName + ' - ' + profession)
}
var person = {
firstName: 'John',
secondName: 'Smith',
age: 33,
children: 3,
profession: 'teacher'
}
printBasicInfo(person)
const obj = {
name: "Fred",
age: 42,
id: 1
}
//simple destructuring
const { name } = obj;
console.log("name", name);
//assigning multiple variables at one time
const { age, id } = obj;
console.log("age", age);
console.log("id", id);
//using different names for the properties
const { name: personName } = obj;
console.log("personName", personName);
Run code snippet
const hero = {
name: 'Batman',
realName: 'Bruce Wayne',
address: {
city: 'Gotham'
}
};
// Object destructuring:
const { realName, address: { city } } = hero;
city; // => 'Gotham'
// Noob [ not good ]
function getFullName(userObj) {
const firstName = userObj.firstName;
const lastName = userObj.lastName;
return `${firstName} ${lastName}`;
}
// master [ yap little bit ]
function getFullName(userObj) {
const { firstName, lastName } = userObj;
return `${firstName} ${lastName}`;
}
// hacker [ i prefer this way ]
function getFullName({ firstName, lastName }) {
return `${firstName} ${lastName}`;
}
// example func call
getFullName({
firstName: 'John',
lastName: 'Duo'
});
({x: oof.x, y: oof.y} = foo);