I have an array structure like this :
const array = [
[
{
"item": { "name": "item1" },
"property": { "name": "property1" }
},
{
"item": { "name": "item2" },
"property": { "name": "property2" }
}
],
[
{
"item": { "name": "item3" },
"property": { "name": "property3" }
}
]
]
If I run this command in pure Javascript:
let count = 0
for(let i = 0; i < array.length; i++) {
for(let j = 0; j < array[i].length; j++) {
count ++
console.log(count)
}
}
Then the output will be like this :
1,2,3
I want to be able to generate the output like above using array.map() in ReactJS and keep those sections. This is the code I'm currently using:
return (
<div className="container">
{array.map((nestedArray, arrayIndex) => (
<div key={arrayIndex} className="array-list">
<h1>Group: {arrayIndex+1}</h1>
{nestedArray.map((items, nestedArrayIndex) => (
<p>Item: {nestedArrayIndex+1}</p>
))}
</div>
))}
</div>
)
And this is the render output (using my css styles):
What is an effective and short way to do it?
I hope I have explained everything clearly
keyattribute on thediv. If you remove thekeythen it will show as you want it to but React will throw an error.