Destructuring Objects in JavaScript: A Beginner's Guide

marianacaldas

Mariana Caldas

Posted on March 5, 2023

Destructuring Objects in JavaScript: A Beginner's Guide

If you've ever found yourself needing to access properties of an object in JavaScript, you may have written code like this:

const data = { name: 'John', age: 30 };
const name = data.name;
const age = data.age;

Enter fullscreen mode Exit fullscreen mode

This code creates an object data with two properties, name and age. The second and third lines then assign the values of these properties to new variables name and age, respectively.

While this code works, it can be a bit verbose, especially if you need to access many properties of the object. Fortunately, JavaScript provides a more concise way to extract values from an object: object destructuring.


What is Object Destructuring?

Object destructuring is a feature in JavaScript that allows you to extract values from an object and assign them to new variables in a single statement. Here's an example:

const data = { name: 'John', age: 30 };
const { name, age } = data;

Enter fullscreen mode Exit fullscreen mode

This code creates an object data with two properties, name and age. The second line then uses object destructuring to create new variables name and age and assign them the values of the name and age properties of the data object, respectively.

Note that the syntax of object destructuring uses curly braces { } to indicate that we're destructuring an object, and the variable names inside the curly braces match the property names of the object we want to extract values from.


Renaming Variables

Sometimes the variable name you want to use for a property doesn't match the property name itself. In such cases, you can use the : syntax to rename the variable as you destructure the object. Here's an example:

const data = { name: 'John', age: 30 };
const { name: fullName, age: yearsOld } = data;

Enter fullscreen mode Exit fullscreen mode

This code creates an object data with two properties, name and age. The second line then uses object destructuring to create new variables fullName and yearsOld and assign them the values of the name and age properties of the data object, respectively. Note that we've used the : syntax to rename the variables as we destructure the object.


Destructuring Nested Objects

Sometimes objects can contain other objects as properties. In such cases, you can use nested destructuring to extract values from the nested objects. Here's an example:

const data = { name: 'John', age: 30, address: { city: 'New York', state: 'NY' } };
const { name, age, address: { city, state } } = data;

Enter fullscreen mode Exit fullscreen mode

This code creates an object data with three properties, name, age, and address. The address property itself is an object with two properties, city and state. The second line then uses nested destructuring to create new variables name, age, city, and state, and assign them the values of the corresponding properties of the data object.

Note that we've used the same syntax as before to extract the name and age properties, but we've added a second level of destructuring to extract the city and state properties of the nested address object.


Destructuring Arrays of Objects

Arrays of objects are a common data structure in JavaScript. Fortunately, you can use array destructuring to extract values from arrays of objects just as easily as you can use object destructuring to extract values from individual objects.

Here's an example:

const data = [
  { name: 'John', age: 30 },
  { name: 'Jane', age: 25 },
  { name: 'Bob', age: 40 }
];
const [ person1, person2, person3 ] = data;

Enter fullscreen mode Exit fullscreen mode

This code creates an array data containing three objects, each with name and age properties. The second line then uses array destructuring to create new variables person1, person2, and person3, and assign them the values of the corresponding objects in the data array.

Note that we've used square brackets [ ] to indicate that we're destructuring an array, and we've used variable names to match the position of the objects in the array.

If you only need to extract certain properties from the objects in the array, you can use object destructuring within the array destructuring syntax. Here's an example:

const data = [
  { name: 'John', age: 30 },
  { name: 'Jane', age: 25 },
  { name: 'Bob', age: 40 }
];
const [ { name: name1 }, { name: name2 }, { name: name3 } ] = data;

Enter fullscreen mode Exit fullscreen mode

The code above creates an array data containing three objects, each with name and age properties. The second line then uses array destructuring to create new variables name1, name2, and name3, and assign them the values of the name properties of the corresponding objects in the data array. Note that we've used object destructuring within the array destructuring syntax to extract only the name properties of the objects in the array.

If you only need to extract one or a few properties from a specific object within the array, you can use a combination of array and object destructuring. Here's an example:

const data = [
  { id: 1, name: 'John', age: 30 },
  { id: 2, name: 'Jane', age: 25 },
  { id: 3, name: 'Bob', age: 40 }
];
const [ , { name: newName } ] = data;

Enter fullscreen mode Exit fullscreen mode

This code creates an array data containing three objects, each with id, name, and age properties. The second line then uses array destructuring to skip over the first object in the data array and destructure the second object, creating a new variable newName and assigning it the value of the name property of the second object in the data array.

Note that we've used a comma , to skip over the first object in the array, and we've used object destructuring to extract the name property of the second object in the array.


Conclusion

Object destructuring is a powerful feature in JavaScript that allows you to extract values from objects and arrays in a concise and readable way. By combining object and array destructuring, you can easily extract values from complex data structures and assign them to new variables.

💖 💪 🙅 🚩
marianacaldas
Mariana Caldas

Posted on March 5, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related