Iterating through a Structure
Question
Application Type
Reactive

I am using a REST Api to get some data and part of the data returns something like the following

"fields": {
    "title": {
      "en-US": "Hello, World!"
    },
    "body": {
      "en-US": "Bacon is healthy!"
    }
  }

I am provided  with a Structure "Field" and I would like to iterate through the Structure and display a "List" of fields. Is there any way that I can do that? Or is it possible to reference  to a Structure attribute through a variable like a list? For example, I have a variable "Fieldname" that holds the attribute name, can I reference to the attribute by Field[Fieldname]?

Hi,

Do you want to iterate on API Response to store it in structure???

For iterating on structure do following,

You need to create local variable of  list of structure and then you can iterate on that list.

You can use same list to display list.

Regards,

Komal

Unfortunately, the API response will depend on a variable that I pass in. I am trying to get an Entry from Contentful. Entries can be of different Content Types and it will return different types of fields. For example, if I retrieve an Entry of "Config" Content Type, it may return 

"fields": {
    "title": {
      "en-US": "Hello, World!"
    },
    "body": {
      "en-US": "Bacon is healthy!"
    }
  }

And if I retrieve and Entry of "Address" Content Type, it may return

"fields": {
    "street": {
      "en-US": "Kallang"
    },
    "zip": {
      "en-US": "123456"
    }
  }

I am trying to have the List UI be very dynamic and display the field name and value according to what is retrieved from the API. For example, "Config" Content Type will show

Title: Hello, World!

Body: Bacon is healthy!


"Address" Content Type will hsow

Street: Kallang

Zip: 123456

So are you storing API Response in single structure and you want iteration on that?

You can add if condition as per your input to API and then store that record into another list and used that list for display purpose.

Regards,

Komal

Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.