-5

I know something is not right with the way I'm trying to read this JSON data. :

[
{
"test": "test",
"listings": [
      {
        "public":
          {
            "item_id": 0,
            "item_img": "",
            "item_title": "example",
            "item_price": 5,
            "item_quantity": 100,
            "item_quality": "New",
            "item_stars": 4.5
          },
        "private": {}
      }
    ]
}
]

What i want is to read the data in "listings" -> "public", like "item_id"...

This wont iterate it just gives me a blank page with no errors nor warnings :

{listingsApi.map(currentListings => (
     <p>{currentListings.listings.public.item_id}</p>
))}

BTW I 100% messed up the map code. Any clue?

Lee Taylor
  • 7,155
  • 14
  • 29
  • 44
Tsujimar
  • 1
  • 3
  • Open the browser console (`Ctrl`+`Shift`+`J`) and your error will be sitting there as plain as day (you'll likely see a message about needing a `key` prop). I can see that you need the `key` prop so React can differentiate the elements in the list. https://epicreact.dev/why-react-needs-a-key-prop/ – code May 14 '22 at 23:18
  • You probably incorrectly returning. Try adding a return in the function body in front of a p tag. – T. Jony May 14 '22 at 23:21
  • @code key as in this?

    {currentListings.listings.public.item_name}

    – Tsujimar May 14 '22 at 23:22
  • @T.Jony Unexpected token. return

    {currentListings.listings.public.item_name}

    | ^
    – Tsujimar May 14 '22 at 23:26
  • You aren't dealing with JSON, you're dealing with a javascript object. JSON is a string that represents an object. – Lee Taylor May 14 '22 at 23:26
  • 2
    `currentListings.listings` is also an array so you need `currentListings.listings[0].public.item_id` – ChrisG May 14 '22 at 23:28
  • @LeeTaylor but how do i fix it tho – Tsujimar May 14 '22 at 23:28
  • You are incorrectly wrapping and returning the values inside an arrow function. Here`s a working example by @Shubham Khatri. https://stackoverflow.com/a/41374730/8474148 – T. Jony May 14 '22 at 23:31
  • @ChrisG thank you so much! It worked. sorry everyone it's 1AM brain fogged. – Tsujimar May 14 '22 at 23:32

0 Answers0