DekGenius.com
JAVASCRIPT
javascript destructuring
const [a, b] = array;
const [a, , b] = array;
const [a = aDefault, b] = array;
const [a, b, ...rest] = array;
const [a, , b, ...rest] = array;
const [a, b, ...{ pop, push }] = array;
const [a, b, ...[c, d]] = array;
const { a, b } = obj;
const { a: a1, b: b1 } = obj;
const { a: a1 = aDefault, b = bDefault } = obj;
const { a, b, ...rest } = obj;
const { a: a1, b: b1, ...rest } = obj;
let a, b, a1, b1, c, d, rest, pop, push;
[a, b] = array;
[a, , b] = array;
[a = aDefault, b] = array;
[a, b, ...rest] = array;
[a, , b, ...rest] = array;
[a, b, ...{ pop, push }] = array;
[a, b, ...[c, d]] = array;
({ a, b } = obj); // brackets are required
({ a: a1, b: b1 } = obj);
({ a: a1 = aDefault, b = bDefault } = obj);
({ a, b, ...rest } = obj);
({ a: a1, b: b1, ...rest } = obj);
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
destructuring
var foo = ['one', 'two', 'three'];
// without destructuring
var one = foo[0];
var two = foo[1];
var three = foo[2];
// with destructuring
var [one, two, three] = foo;
js destructuring
let [person, age] = ['John', 26]
let msg = `${person} is ${age} years old`
console.log(msg)
destructuring in javascript
/*
Array Destructuring: The following example shows us how to convert all
the elements of an array to a variable.
Object Destructuring: The following example shows us how to convert all
the properties of an object into a variable.
*/
//Array Destructuring
const friends = ['Bill', 'Gates'];
const [firstName, secondName] = friends;
console.log(secondName); //Gates
//Object Destructuring
const person = {name: "Bill Gates", age: 17, phone: "123456789"};
const {name} = person;
console.log(name); //Bill Gates
JavaScript Destructuring
// before you would do something like this
const person = {
name: 'Sara',
age: 25,
gender: 'female'
}
let name = person.name;
let age = person.age;
let gender = person.gender;
console.log(name); // Sara
console.log(age); // 25
console.log(gender); // female
destructuring js
console.log('Hello World') // Hello World
const {log} = console;
log('Hello World') // Hello World
js-destructuring
const getTemperature = ({ celsius = 15 } = {}) => celsius;
destructuring javascript
let a, b, rest;
[a, b] = [10, 20];
console.log(a); // 10
console.log(b); // 20
[a, b, ...rest] = [10, 20, 30, 40, 50];
console.log(a); // 10
console.log(b); // 20
console.log(rest); // [30, 40, 50]
({ 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}
js destructuring explained
const { identifier } = expression;
js-destructuring
const getTemperature = (atticData) => atticData?.celsius ?? 15;
// insted do
const getTemperature = ({ celsius = 15 } = {}) => celsius;
// https://realworldjs.medium.com/never-use-the-dot-operator-in-javascript-ee0339d85fb
js destructuring
/*simple example*/
const [name, age] = ["malek","25"];
destructuring
const doggie = {
name: 'Buzz',
breed: 'Great Pyrenees',
furColor: 'black and white',
activityLevel: 'sloth-like',
favoriteFoods: {
meats:{
ham: 'smoked',
hotDog: 'Oscar Meyer',
},
cheeses:{
american: 'kraft'
}
}
};
const { ham, hotDog } = doggie.favoriteFoods.meats;
ham; // => "smoked"
hotDog; // => "Oscar Meyer"
Destructuring in javascript
const { name, age, job } = { name: 'abrar', age: 24, job: 'web-developer' }
console.log(name, age, job)
js destructuring
function calculatefood (food, tip) {
tip_percent = tip / 100;
tip_amount = food * tip_percent;
total = food + tip_amount;
return [tip_amount, total];
}
const [tip, sum] = calculatefood(100, 20);
console.log(tip);
console.log(sum);
© 2022 Copyright:
DekGenius.com