//Without destructuringconst myArray =["a","b","c"];const x = myArray[0];const y = myArray[1];console.log(x, y);// "a" "b"//With destructuringconst myArray =["a","b","c"];const[x, y]= myArray;// That's it !console.log(x, y);// "a" "b"
// In an array destructuring from an array of length N specified on the right-hand side of the assignment, if the number of variables specified on the left-hand side of the assignment is greater than N, only the first N variables are assigned values. The values of the remaining variables will be undefined.const foo =['one','two'];const[red, yellow, green, blue]= foo;console.log(red);// "one"console.log(yellow);// "two"console.log(green);// undefinedconsole.log(blue);//undefined
const user ={id:42,isVerified:true}// grabs the property by name in the object, ignores the orderconst{ isVerified, id }= user;console.log(isVerified);// > true
// destructuring assignment when passing// an object of properties to a functionfunctionexample({userOptions:{query, all =true, sort}={}, array}){console.log(query, all, sort)// something false ascending}// for clarity, the above example is destructured the same as thisfunctionexample({userOptions, array}){const{query, all =true, sort}= userOptions ||{}}const userOptions ={query:'something',all:false,sort:'ascending'}const array =[]example({userOptions, array})
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);
How to destructure arrays and objects in javascript?
// Array to destructureconst arr =["Wissam","Fawzi","Darwich","Fawaz"];// First element, second element, and then remaining onesconst[firstName, middleName,...restOfName]= arr;console.log(firstName);// Wissamconsole.log(middleName);// Fawziconsole.log(restOfName.join(" "));// Darwich Fawaz// Object to destructureconst socialMedia ={twitter:"@wissam_f",facebook:"https://www.facebook.com/wissam.fawaz",linkedin:"https://www.linkedin.com/in/wissam-fawaz-6b440839/",};// To extract values, property keys should be usedconst{twitter, linkedin}= socialMedia;// Twitter: @wissam_fconsole.log("Twitter:", twitter);// LinkedIn: https://www.linkedin.com/in/wissam-fawaz-6b440839/console.log("LinkedIn:", linkedin);
const arrValue =['one','two','three'];// destructuring assignment in arraysconst[x, y, z]= arrValue;console.log(x);// oneconsole.log(y);// twoconsole.log(z);// three
/*
* 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: 1console.log(b)// prints out: 2
/*
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 Destructuringconst friends =['Bill','Gates'];const[firstName, secondName]= friends;console.log(secondName);//Gates//Object Destructuringconst person ={name:"Bill Gates",age:17,phone:"123456789"};const{name}= person;console.log(name);//Bill Gates
functionprintFirstAndSecondElement([first, second]){console.log('First element is '+ first +', second is '+ second)}functionprintSecondAndFourthElement([, second,, fourth]){console.log('Second element is '+ second +', fourth is '+ fourth)}var array =[1,2,3,4,5]printFirstAndSecondElement(array)printSecondAndFourthElement(array)
// before you would do something like thisconst person ={name:'Sara',age:25,gender:'female'}let name = person.name;let age = person.age;let gender = person.gender;console.log(name);// Saraconsole.log(age);// 25console.log(gender);// female
// 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.
// assigning object attributes to variablesconst person ={name:'Sara',age:25,gender:'female'}let name = person.name;let age = person.age;let gender = person.gender;console.log(name);// Saraconsole.log(age);// 25console.log(gender);// female
// destructuring assignment in javascript// object destructuringlet person ={name:"Chetan",age:30,country:"India"};const{ name, age }= person;console.log(name);//expected output: "Chetan"console.log(age);//expected output: 30console.log(country);//expected output: Uncaught ReferenceError: country is not defined// Array destructuringconst num =[1,2,3];const[one, two, three]= num;console.log(one);// 1console.log(two);// 2console.log(three);// 3