Destructuring objects and arrays
Destructuring is a convenient way of creating new variables by extracting some values from data stored in objects or arrays.
To name a few use cases, destructuring can be used to destructure function parameters or this.props in React projects for instance.
Explanation with sample code
Let's consider the following object for all the samples:
| const person = {
firstName: "Nick",
lastName: "Anderson",
age: 35,
sex: "M"
}
|
Without destructuring
| const first = person.firstName;
const age = person.age;
const city = person.city || "Paris";
|
With destructuring, all in one line:
| const { firstName: first, age, city = "Paris" } = person; // That's it !
console.log(age) // 35 -- A new variable age is created and is equal to person.age
console.log(first) // "Nick" -- A new variable first is created and is equal to person.firstName
console.log(firstName) // ReferenceError -- person.firstName exists BUT the new variable created is named first
console.log(city) // "Paris" -- A new variable city is created and since person.city is undefined, city is equal to the default value provided "Paris".
|
Note : In const { age } = person;
, the brackets after const keyword are not used to declare an object nor a block but is the destructuring syntax.
Destructuring is often used to destructure objects parameters in functions.
Without destructuring
| function joinFirstLastName(person) {
const firstName = person.firstName;
const lastName = person.lastName;
return firstName + '-' + lastName;
}
joinFirstLastName(person); // "Nick-Anderson"
|
In destructuring the object parameter person, we get a more concise function:
| function joinFirstLastName({ firstName, lastName }) { // we create firstName and lastName variables by destructuring person parameter
return firstName + '-' + lastName;
}
joinFirstLastName(person); // "Nick-Anderson"
|
Destructuring is even more pleasant to use with arrow functions:
| const joinFirstLastName = ({ firstName, lastName }) => firstName + '-' + lastName;
joinFirstLastName(person); // "Nick-Anderson"
|
Let's consider the following array:
| const myArray = ["a", "b", "c"];
|
Without destructuring
| const x = myArray[0];
const y = myArray[1];
|
With destructuring
| const [x, y] = myArray; // That's it !
console.log(x) // "a"
console.log(y) // "b"
|
Useful resources