DekGenius.com
JAVASCRIPT
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"]
javascript object destructuring
//simple example with object------------------------------
let obj = {name: 'Max', age: 22, address: 'Delhi'};
const {name, age} = obj;
console.log(name);
//expected output: "Max"
console.log(age);
//expected output: 22
console.log(address);
//expected output: Uncaught ReferenceError: address is not defined
// simple example with array-------------------------------
let a, b, rest;
[a, b] = [10, 20];
console.log(a);
// expected output: 10
console.log(b);
// expected output: 20
[a, b, ...rest] = [10, 20, 30, 40, 50];
console.log(rest);
// expected output: Array [30,40,50]
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;
javascript object destructing
const employee = {name: ‘ANE01’, email: ‘Anna@example.com’, phone:’0112–345–6789'};
//with destucturing
const {name, email, phone} = employee;
//without destucturing
const name = employee.name;
const email = employee.email;
const phone = employee.phone;
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}
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);
destructuring an object js
const user = { id: 42, isVerified: true }
// grabs the property by name in the object, ignores the order
const { isVerified, id } = user;
console.log(isVerified);
// > true
Object destructuring
Object Destructuring =>
//
The destructuring assignment syntax is a JavaScript expression that makes it
possible to unpack values from arrays,
or properties from objects, into distinct variables.
//
example:
const user = {
id: 42,
is_verified: true
};
const {id, is_verified} = user;
console.log(id); // 42
console.log(is_verified); // true
Destructuring of object in ES6
function printBasicInfo({firstName, secondName, profession}) {
console.log(firstName + ' ' + secondName + ' - ' + profession)
}
var person = {
firstName: 'John',
secondName: 'Smith',
age: 33,
children: 3,
profession: 'teacher'
}
printBasicInfo(person)
destructured object
let renseignement = ['voleur' , '10' , 'spécialité'] ;
let [classe , force, magie] = renseignement ;
console.log(classe) ;
console.log(force) ;
console.log(magie) ;
destructuring object
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
object destructuring ES6
const person = {
name: 'John',
age: 34,
hobbies: {
read: true,
playGames: true
}
}
let {name, hobbies: {read, playGames}} = person;
console.log(person);
console.log(name);
console.log(read,playGames);
object destructuring
let a, b, rest;
[a, b] = [10, 20];
console.log(a);
// expected output: 10
console.log(b);
// expected output: 20
[a, b, ...rest] = [10, 20, 30, 40, 50];
console.log(rest);
// expected output: Array [30,40,50]
javascript destructing
/*
* On the left-hand side, you decide which values to unpack from the right-hand
* side source variable.
*
* This was introduced in ES6
*/
const x = [1, 2, 3, 4, 5]
const [a, b] = x
console.log(a) // prints out: 1
console.log(b) // prints out: 2
object destructuring example
const hero = {
name: 'Batman',
realName: 'Bruce Wayne',
address: {
city: 'Gotham'
}
};
// Object destructuring:
const { realName, address: { city } } = hero;
city; // => 'Gotham'
object destructuring ES6
let person = {
name: 'John',
age: 21,
gender: 'male'
}
let { name, age, gender } = person;
console.log(name, age, gender);
js object destructuring with defaults
// Taken from top stack overflow answer
const { dogName = 'snickers' } = { dogName: undefined }
console.log(dogName) // what will it be? 'snickers'!
const { dogName = 'snickers' } = { dogName: null }
console.log(dogName) // what will it be? null!
const { dogName = 'snickers' } = { dogName: false }
console.log(dogName) // what will it be? false!
const { dogName = 'snickers' } = { dogName: 0 }
console.log(dogName) // what will it be? 0!
js object destructuring
const { [propName]: identifier } = expression;
how to use object destructuring
// 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'
});
object destructuring in javascript
const hero = {
name: 'Batman'
};
// Object destructuring:
const { name: heroName } = hero;
heroName; // => 'Batman'
javascript object destructuring
// In this syntax:
let { property1: variable1, property2: variable2 } = object;
// The identifier before the colon (:) is the property of the object and the identifier after the colon is the variable.
object destructuring ES6
let person = {
name: 'John',
age: 21,
gender: 'male'
}
let { name : name1, age: age1, gender:gender1 } = person;
console.log(name1, age1, gender1);
destructuring an object in JS
let person = {
firstName: 'John',
lastName: 'Doe'
};
Code language: JavaScript (javascript)
object destructuring
const user = {
name: "Jenny",
age: "12",
hobbies: ["Sport", "Programmieren", "essen"],
};
const { age } = user;
console.log(age);
© 2022 Copyright:
DekGenius.com