js object destructuring
const hero = {
name: 'Batman',
realName: 'Bruce Wayne'
};
const { name, realName } = hero;
name; // => 'Batman',
realName; // => 'Bruce Wayne'
js object destructuring
const hero = {
name: 'Batman',
realName: 'Bruce Wayne'
};
const { name, realName } = hero;
name; // => 'Batman',
realName; // => 'Bruce Wayne'
object destructuring
const book = {
title: 'Ego is the Enemy',
author: 'Ryan Holiday',
publisher: {
name: 'Penguin',
type: 'private'
}
};
const {title: bookName = 'Ego', author, name: {publisher: { name }} = book, type: {publisher: { type }} = book } = book;
object destructuring javascript
// destructuring object & nested object & combine object into single object
let user = {
name: 'Mike',
friend: ["John", "Paul", "Jimmy"],
location: {
region:"England",
country:"United Kingdom"
},
aboutMe: {
status: "Single",
pet: "Dog",
}
}
const { name, friend, location, aboutMe: {status , pet} } = user;
console.log(name); // output: "Mike"
console.log(friend); // output: [ 'John', 'Paul', 'Jimmy' ]
console.log(location); // output: { region: 'England', country: 'United Kingdom' }
console.log(status); // output: "Single"
console.log(pet); // output: "Dog"
//Combining Obj
const newUser = {
...user,
car: {
make: "Buick",
year: 2012,
}
}
console.log(newUser)
// output user obj + car object into one
// {
// name: 'Mike',
// friend: [ 'John', 'Paul', 'Jimmy' ],
// location: { region: 'England', country: 'United Kingdom' },
// aboutMe: { status: 'Single', pet: 'Dog' },
// car: { make: 'Buick', year: 2012 }
// }
//Bonus destructuring from object of array
const {friend: [a, ...b]} = user
console.log(a) // output: "John"
console.log(b) // output: ["Paul", "Jimmy"]
destructuring objects
({ a, b } = { a: 10, b: 20 });
console.log(a); // 10
console.log(b); // 20
// Stage 4(finished) proposal
({a, b, ...rest} = {a: 10, b: 20, c: 30, d: 40});
console.log(a); // 10
console.log(b); // 20
console.log(rest); // {c: 30, d: 40}
How to Destructuring Array in Javascript?
//destructuring array
const alphabet = ['a', 'b', 'c', 'b', 'e'];
const [a, b] = alphabet;
console.log(a, b);
//Expected output: a b
Javascript array destructuring
let [a, b] = [9, 5]
[b, a] = [a, b]
console.log(a, b);
//Expected output: 5,9
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us