I have an array of Objects in my MongoDB
and I want to map it to a Table in React
. I have tried but it is giving undefined in the browser console and it is not mapping. when I console.log
the vm it gives the following data:
How can you will help me?
[
{
"Virtual_Machines": {
"Debian": {
"VM_Name": "Debian",
"VM_Location": "eastus",
"VM_Disk_Name": "Debian_OsDisk_1_b890f5f5c42647549c881c0706b85201",
"VM_Publisher_Info": {
"publisher": "debian",
"offer": "debian-11",
"sku": "11-gen2",
"version": "latest"
},
"Vm_Disk_Type": "Standard_D2s_v3",
"VM_Encryption": null
},
"Ubuntu": {
"VM_Name": "Ubuntu",
"VM_Location": "eastus",
"VM_Disk_Name": "Ubuntu_disk1_0610e0fde49b481490ef0a069a03b460",
"VM_Publisher_Info": {
"publisher": "canonical",
"offer": "0001-com-ubuntu-server-focal",
"sku": "20_04-lts-gen2",
"version": "latest"
},
"Vm_Disk_Type": "Standard_D2s_v3",
"VM_Encryption": null
}
}
},]
<table className="audit table">
<thead className="table-th">
<tr>
<th>Name</th>
<th>Location</th>
<th>Encryption</th>
</tr>
</thead>
<tbody className="table-body">
{vm.map((x) => (
<tr>
<td>{x.Virtual_Machines}</td>
<td>{x.VM_Location}</td>
<td>{x.VM_Encryption}</td>
</tr>
))}
</tbody>
</table>
2
Answers
Your mapping is wrong, you have four levels / nesting in your JSON including Virtual_Machines, machine type, machine attributes and VM_Publisher_Info.
You have mixed Virtual_Machines with machine attributes (VM_Location, VM_Encryption) that is wrong.
A similar case is:
mapping a nested array from json in React js
Please change this part like below