Iterating list of objects

So I’ve pulled some data into my app from the Google places API (with comments in //) with the following schema:

{
“html_attributions”: [ ],
“result”: { //an object with 14 properties
“address_components”: [ //this is a list of objects with 3 properties
{
“long_name”: “14”,
“short_name”: “14”,
“types”: [ //this is a list of texts
“street_number”
]
},
{
“long_name”: “Cumbor Way”,
“short_name”: “Cumbor Way”,
“types”: [
“route”
]
},
{
“long_name”: “Samson”,
“short_name”: “Samson”,
“types”: [
“locality”,
“political”
]
},
//etc

but cant for the life of me figure out how to extract, for example, the “long_name” where a “types” is equal to “locality”. I’m thinking it would use LOOKUP and INDEX_OF functions but I can’t get my head around it… Any ideas?

LOOKUP(source.object.address_components[], “long_name”)

The above gives me the “long_name” value but first I need the … Feels like a chicken and the egg problem…

My brain thinks in for-loops and whiles which would make this problem easy. Have looked through other iterating posts on here, read the docs and watched a million videos but am finding it hard when the formula calculator screams red at me!

Thought I would post for anyone else trying to do this:

LOOKUP(FIND(source.object.address_components, IS_IN_ARRAY(item.types, “locality”)), “long_name”)

To summarise what I was tyring to do… It’s basically an autocomplete for a user entering their address. Firrst I setup the endpoints in DATA up the top as using REST. They type their address and the onchange event for that input field calls the autocomplete api to list some suggestions in a container below that repeats its values based on the Get_Collection call to to the api. When a user clicks the address they want (component tap in the logic), it hides the container and then calls he Get_Record api call using the place_id.

The JSON data this returns is then used to set some variables I had setup but working with JSON is weird when you’re not used to being able to use loops as it was a lot of objects and lists all nested within each other and appgyver has no direct way to loop. You can do it by using the logic flows or by triggering some javascript but I think if formulas can be used it will be easier to read down the track as it’s not much code… Functions like MAP, FIND, etc all allow you to traverse through an array as you would with a loop and a lot of what I learnt came from looking at javascript videos which have very similar functions…

So after Get record in the logic that is then connected to set page variable where the assigned value is mapped to the function LOOKUP(FIND(source.object.address_components, IS_IN_ARRAY(item.types, “locality”)), “long_name”) . Obviously that one is for “locality” and I would then have the same formula for every mapped item that I need with “locality” changed to “street_number” or “route” etc.

1 Like