If you have an API request in your app that returns an array, you can filter the results before displaying them in the page. For example, you may want to test a particular field in each array entry to determine whether or how to display the data representing that item.
With a request returning an array added to a page in your app:
- Open the request Events and select the relevant status code (typically this will be
200: OK). Click the plus button to add an Action, selecting Iterate through Array. Edit the Action.
Save the Action.
- In the nested Item Event, add an Action, choosing If… Else.
- In the first input, select the field from the array item that you want to test.
- In the center input, select an operator, for example to carry out a comparison test.
- In the third input, select or enter a value to test the array item field against, which you can enter manually or pull from the sources in your app.
- In the nested True and False Events, add Actions to display your array fields depending on the result of the test, for example using Set Value.
- Note that you may need to add more Actions before the If… Else in the Item Event to make your array fields accessible inside the True and False Events. Event Data is only available within the scope of the relevant Event. For example, this sequence of Actions filters locations returned from Google Places API, only displaying pins for array entries that meet a certain condition. By saving the Item data to local variables in each iteration, the Actions inside the If… Else nested Events can access values for each place throughout the iteration. When adding new Actions to an Event, use the arrow buttons to set the order of execution.
You can also use status code Events to tailor page display to response fields that are not in arrays. To display images from an API array, use Element loaded Events. You can alternatively check for values in API array results and compare them. For more on location and mapping Actions, see the native functionality tutorials.