How To Destructure Object Properties Using Array - Map in React
How To Destructure Object Properties Using Array - Map in React
map() in React
Forum Donate
https://www.freecodecamp.org/news/destructure-object-properties-using-array-map-in-react/ 1/9
11/14/23, 4:53 PM How to Destructure Object Properties Using array.map() in React
The snippet above pretty much fulfills its purpose. But what if you
have to map through an array of objects with multiple properties?
Say, an array like the one below?
const employees = [
{
name: 'Saka Manje',
address: '14, cassava-garri-ewa street',
gender: 'Male',
},
{
name: 'Wawawa Warisii',
address: '406, highway street',
gender: 'Male',
https://www.freecodecamp.org/news/destructure-object-properties-using-array-map-in-react/ 2/9
11/14/23, 4:53 PM How to Destructure Object Properties Using array.map() in React
},
] Forum Donate
To be concise, let's stick with two items in the array. Now, let's use
the same approach that we used in the previous snippet.
While the approach in the snippet above looks perfectly okay, you
might wonder – "What happens when I get deeply nested objects as
data from an endpoint?"
In this section, we'll take a look at how we can achieve the same
result without using dot notation to access the properties in the
https://www.freecodecamp.org/news/destructure-object-properties-using-array-map-in-react/ 3/9
11/14/23, 4:53 PM How to Destructure Object Properties Using array.map() in React
array.
Forum Donate
const person = {
name: 'Adrian Tojubole',
role: 'Lead Engineer',
salary: '$130k/year',
}
console.log(person.name);
console.log(person.role);
console.log(person.salary);
With that out of the way, we'll take this pattern and use it whenever
we want to use the .map() method in React. Take the array of
objects below, for example:
https://www.freecodecamp.org/news/destructure-object-properties-using-array-map-in-react/ 4/9
11/14/23, 4:53 PM How to Destructure Object Properties Using array.map() in React
But, when you destructure the properties in that object, the syntax
looks somewhat like this:
employeesData.map(
({ name, address, attributes: { height, hairColor, eye }, gender },
return name, address, height, hairColor, eye
https://www.freecodecamp.org/news/destructure-object-properties-using-array-map-in-react/ 5/9
11/14/23, 4:53 PM How to Destructure Object Properties Using array.map() in React
}
) Forum Donate
Now, that you have an idea of how it works, it is time to place this
.map() into a React component, then return the corresponding
properties.
Wrapping Up
https://www.freecodecamp.org/news/destructure-object-properties-using-array-map-in-react/ 6/9
11/14/23, 4:53 PM How to Destructure Object Properties Using array.map() in React
With this approach you can save a lot of time you spend using dot
Forum Donate
notation to access object properties. This is useful as time goes on,
because you may begin
Learn to interface
to code with GraphQL
— free 3,000-hour APIs, and some of
curriculum
these APIs are commonly known to have deeply nested objects
being returned as data.
Thank you for reading this tutorial. I hope you found it helpful.
Caleb Olojo
Web developer
If you read this far, thank the author to show them you care.
Say Thanks
https://www.freecodecamp.org/news/destructure-object-properties-using-array-map-in-react/ 7/9
11/14/23, 4:53 PM How to Destructure Object Properties Using array.map() in React
Forum Donate
Our mission: to help people learn to code for free. We accomplish this by creating thousands of
videos, articles, and interactive coding lessons - all freely available to the public. We also have
thousands of freeCodeCamp study groups around the world.
Donations to freeCodeCamp go toward our education initiatives, and help pay for servers,
services, and staff.
Trending Guides
https://www.freecodecamp.org/news/destructure-object-properties-using-array-map-in-react/ 8/9
11/14/23, 4:53 PM How to Destructure Object Properties Using array.map() in React
About Alumni Network Open Source Shop Support Sponsors Academic Honesty
https://www.freecodecamp.org/news/destructure-object-properties-using-array-map-in-react/ 9/9