How to actually get all values from this array?

I have this data structure, but I don’t know how to access to these "events" because when I do the console.log(data.opis) I get undefined as a output for that part inside of console.

What I am trying to achieve later on is that I will have one div and add all values onto website, but in order (this is actually from the one of the post express things so, they change status (btw. I don’t know how many events could be the maximum – so I would have to plan to print out all the values from the events actually)

const data = {
  "sifra": "",
  "datumSlanja": "2022-09-13T14:28:49.143+0200",
  "datumPrijema": "2022-09-21T08:19:53.584+0200",
  "opisPosiljke": null,
  "tezina": "0.50",
  "brojPaketa": 1,
  "vrednostPosiljke": 30,
  "status": 20,
  "statusOpis": "DOSTAVLJENA",
  "centar": "TC-Mostar",
  "mjesto": "Mostar",
  "posiljkuPreuzeo": "VRAĆA SE",
  "events": [{
      "opis": "PREUZETA",
      "centar": "TC-Sarajevo",
      "datum": "2022-09-13T14:28:49.143+0200"
    },
    {
      "opis": "Pošiljka razdužena u centru slanja",
      "centar": "TC-Sarajevo",
      "datum": "2022-09-13T15:59:48.134+0200"
    },
    {
      "opis": "Pošiljka kod kurira na dostavi",
      "centar": "TC-Mostar",
      "datum": "2022-09-14T08:35:38.744+0200"
    },
    {
      "opis": "DOGOVORENO PRIMALAC",
      "centar": null,
      "datum": "2022-09-14T12:02:33.325+0200"
    },
    {
      "opis": "ODGOĐENA",
      "centar": "TC-Mostar",
      "datum": "2022-09-14T15:06:39.149+0200"
    }
  ]
};

console.log(data.datumSlanja)
console.log(data.sifra)
console.log(data.datumPrijema)
console.log(data.centar)

>Solution :

You are working with an object, so you have to do next to get events value:

data.events.map(item => { console.log(item.opis); }):

In this case you will have all opis values from events array.

const data = {
  "sifra": "",
  "datumSlanja": "2022-09-13T14:28:49.143+0200",
  "datumPrijema": "2022-09-21T08:19:53.584+0200",
  "opisPosiljke": null,
  "tezina": "0.50",
  "brojPaketa": 1,
  "vrednostPosiljke": 30,
  "status": 20,
  "statusOpis": "DOSTAVLJENA",
  "centar": "TC-Mostar",
  "mjesto": "Mostar",
  "posiljkuPreuzeo": "VRAĆA SE",
  "events": [{
      "opis": "PREUZETA",
      "centar": "TC-Sarajevo",
      "datum": "2022-09-13T14:28:49.143+0200"
    },
    {
      "opis": "Pošiljka razdužena u centru slanja",
      "centar": "TC-Sarajevo",
      "datum": "2022-09-13T15:59:48.134+0200"
    },
    {
      "opis": "Pošiljka kod kurira na dostavi",
      "centar": "TC-Mostar",
      "datum": "2022-09-14T08:35:38.744+0200"
    },
    {
      "opis": "DOGOVORENO PRIMALAC",
      "centar": null,
      "datum": "2022-09-14T12:02:33.325+0200"
    },
    {
      "opis": "ODGOĐENA",
      "centar": "TC-Mostar",
      "datum": "2022-09-14T15:06:39.149+0200"
    }
  ]
};

data.events.map(item => {
  console.log(item.opis);
});

Leave a Reply