0

How do I destructure this JSON object, so I can get the "Name" property names. I'm trying to get them showed on my DOM, but I have no clue how to access "Warehouses" object then it's arrays, and the objects into arrays.

{
    "ErrorCode": 0,
    "ErrorMessage": null,
    "Warehouses": [
        {
            "Code": null,
            "Name": "Depozit Fabrica",
            "WarehouseID": "cb4fbab4-b8db-4807-a2b0-fad710f1fd9e"
        },
        {
            "Code": "3",
            "Name": "Depozit Magazin",
            "WarehouseID": "dfa08a15-e3a0-4d43-8af6-c24a9d43101c"
        },
        {
            "Code": null,
            "Name": "Depozit Ograda",
            "WarehouseID": "0dc8318d-305c-4e09-a31c-aa6fd44bf2ca"
        }
    ]
}
Dipzera
  • 95
  • 9

1 Answers1

1

Solved it myself.

const url = "\url";
async function getData() {
  const response = await fetch(url);
  const data = await response.json();
  const { Warehouses } = data;

  // Loop through all warehouse's items

  for (var i = 0; i < Warehouses.length; i++) {
     const dataItems = Warehouses[i].Name;
   }
}
getData();

Now the problem is I don't know how to show every Name element in DOM separately, like in a

tag. I want to give them a click event listener further, so I can get more json data in DOM.

Dipzera
  • 95
  • 9