Hatim Fayez
Hatim Fayez

Reputation: 231

How to iterate over an array of JSON

Need a hand in this. I know it might be very easy but I am really stacked I am trying ti iterate an array of response

This is the log of data

Object {
  "address": "",
  "city": "",
  "country": "",
  "country_code": "E",
  "daylight": "0",
  "for": "daily",
  "items": Array [
    Object {
      "name": "John",
      "age": "28",
      "Job": "worker",

    }
  ]}

I am trying to iterate over that array of items and did everything possible nothing works I tries to map, find and looping it's working well but after reloading the page it's giving error >state.map is not a function< or whatever the function

I tried also

const name = state.items.name
const person = state.items;
const name = person.name;

I tried to set this array as a state and then iterate, nothing is working

Need help how to get the data inside without errors

Note : This data is coming from async function to fetch data so it takes some time to be getting the data first.

Upvotes: 0

Views: 197

Answers (2)

Wael Fadhel
Wael Fadhel

Reputation: 151

well, you can use a loading component that waits for your async call until it ends and then render your actual component, or you can simply declare it this way const items = response.items? response.items: []

Upvotes: 1

Re-Angelo
Re-Angelo

Reputation: 537

Set your initial state as empty array [ ] and when its fetched, set the statenwith the data

Upvotes: 0

Related Questions