Destructuring in Js
September 20, 2020
Destructuring in js π
Destructuring feature introduce in es6. Itβs use to unpack array and object into variable.
Letβs jump on the code
1. Array Destructuring [π,π,π]
itβs looks like this π¬
let [cake,burger,pizza] = [π°,π,π]
For Example:
// Destructuring is the shorter way to write below code:
let names = ["Steave","Roger"];
let firstName = names[0];
let lastName = names[1];
console.log(firstName); //Steave
console.log(lastName);//Roger
// By using destructuring it's become too easy and short
let [firstName,lastName] = names;
console.log(firstName,lastName); // Steave Roger
-
Ignoring elements using commas
For example:
let [one,,three,four] = [1,2,3,4];
// The second element of the array is skipped
// Because there is no any variable define for them
console.log(one,three,four); // 1 3 4
-
Default Values
Default values will appears if the value is not provided.
For Example:
let firstName,secondName;
[firstName = "Natasha",secondName = "Pepper Potts"] = ["Hulk","IronMan"];
//In above array we have two super hero
// Hulk and Iron Man
// So now firstName have Hulk and secondName have IronMan
// Because both values present into array
console.log(firstName,secondName); // Hulk IronMan
// Now we remove IronMan from above array
[firstName = "Natasha",secondName = "Pepper Potts"] = ["Hulk"];
// Now secondName have Pepper Potts as default value
console.log(firstName,secondName); // Hulk Pepper Potts
-
Destructuring in nested array
For Example:
let randomArray = [
["cake","donut",["Orio"]],
["Kitkat"]
];
// first we get the cake
let [[cake]] = randomArray;
console.log(cake); // cake
// get the orio
let [[,,[orio]]] = randomArray;
console.log(orio); // Orio
// get the kitkat
let [,[kitkat]] = randomArray;
console.log(kitkat); // "Kitkat"
-
swap variable
For Example:
let firstName = "utkarsh";
let lastName = "srivastava";
// now let swap with destructuring
[firstName,lastName] = [lastName,firstName];
console.log(firstName,lastName); // srivastava utkarsh
-
some more example
For Example:
// split
let [firstName,lastName] = "Steave roger".split(" ");
console.log(firstName,lastName); // Steave roger
// object.entries()
let info = {
name:"Jake",
age:23,
};
// here we used destructuring
for(let [key,value] of Object.entries(info)){
console.log(key,value); // name Jake
// age 23
}
// with set
let [one,two,three] = new Set([1,2,3]);
console.log(one,two,three);
// Rest parameter
let [first,...rest] = "I write code".split(" ");
console.log(first,rest); // I ["write","code"]
2. Object Destructuring
In object destructuring we unpack object on the basis of there keys.
Letβs wrap it faster
For Example:
let user = {
name:"John doe",
location:{
lat:78.4665,
lng:47.123190
};
extra:true,
items:[π°,π]
};
// First we see the simple case
// Get the value of name
let {name} = user;
console.log(name); // "John doe"
// we can also assign another name to the object property
let {name:N} = user;
console.log(N); // "John doe"
// Default value in destructuring
// age does not exit inside user so i will give default value
let {age = 23} = user;
console.log(age); // 23
-
Nested destructuring
let user = {
name:"John doe",
location:{
lat:78.4665,
lng:47.123190
};
extra:true,
items:[π°,π]
};
// get the lat lng
let {location:{lat,lng}} = user;
console.log(lat,lng); // 78.4665 47.123190
// get the cake and meat
let {items:[cake,meat]} = user;
console.log(cake,meat); // π° π
I think thatβs it for destructuring
Please subscribe our news letter
If you like what you just read. Please subscribe to our newsletter. So new content will be delivered to your inbox :)