I have an array of data:
const arr = [
{
title: "title1",
content: [
{
id: "home",
name: "Home",
},
{
id: "services",
name: "services",
},
{
id: "Appointments",
name: "Appointments",
},
],
},
{
title: "title2",
content: [
{
id: "about us",
name: "about us",
},
{
id: "contact",
name: "contact",
},
{
id: "blog",
name: "blog",
},
],
},
];
I want to map through all of it.
I’ve tried using nested maps
and adding key prop
to each tag
{arr.map((element) => (
<div key={element.name}>
<h4 key={element.name}>{element.title}</h4>
<ul key={element.name}>
{element.content.map((index) => (
<li key={index.id}>
{index.name}
</li>
))}
</ul>
</div>
))}
I’ve tried using nested maps
and it works fine and shows the array but I got this warning
Warning: Each child in a list should have a unique "key" prop.
How should I solve this warning?
2
Answers
This Adds index to array map.
When any loop or nested loop begins
key={index}
should be placed in theparent element tag.
It should work: