Iterating though an array

Hi, I’ve got a restAPI call that brings master data into my app.

In the app itself I want to use that master data to populate a number of arrays each of which contain a subset of that data. I can’t find out where and how I should do that?

Can you help?

Oli

You might want to split the data into multiple variables using the logic canvas.

e.g. after you’ve fetched the data you set it to variables and use formulas to split that data however you like.

SELECT function could be useful in subsetting the data.

3 Likes

I still can’t quite figure out how to do this. I’ve got arrays of data coming in from an api call.
The array has strings and objects.

I want to create a new page variable array with a subset of the data from the original array, the key issue being that I want some information from the objects contained in the array, but I can’t for the life of me work out how to do that.

I can create arrays of strings, but I want to create an array of key:value pairs of my choosing and I can’t workout the process to stepwise iterate over an array.

It’s driving me insane.

Can you post a sample of your data and the desired end result so we can better help you out?

So, one thing I want to do is have a new array that contains a the same 19 items but that only contains an “id” : string and a “Country” string. Then I want to add three more keys from another array.

are these elements from two arrays under the same ID? If they are you could try a formula something like this

MAP(pageVars.list1, MERGE([{id: item.id, country: item.fields.country}, PICK_KEYS(FIND_BY_KEY(pageVars.list2, "id", item.id), ["beer", "bar"])]))

which builds an array of objects like this [{id: 1, country: "Finland", beer: "karhu", bar: "heinähattu"}], where keys beer and bar are from a list2 and country and id from list1

1 Like

That’s really useful - I’ll try that… the other thing I’m struggling to do is this: If I have a page variable that has a schema that is an array of objects, how do I populate those objects with data from a second array?

Another question though - why is MERGE not on the list of Array functions? And if it’s not on the list - what other functions are not on the list? - DOH - because it’s an object function!!! sorry

For the first question, I guess it depends on the source and target data, but just MAP should be enough to convert any kind of array of data into another kind of array of data.

I think I’m getting there slowly - It’s just that my brain can’t really cope with the difference between arrays and objects.

for example - I have an array of objects. Each object has a number of values - how do I change the value of one of the object items in the array?

It takes a bit to get used to, but once it clicks, they are super powerful concepts! :slight_smile:

If I have an array in my pageVars.array like

[{id: 0, name: "Oranges"}, {id: 1, name: "Apples"}, {id: 2, name: "Kiwis"}] 

I can change just the value of the second object with

MAP(pageVars.array, IF(item.id === 1, SET_KEY(item, "name", "Bananas"), item))

This results in

[{id: 0, name: "Oranges"}, {id: 1, name: "Bananas"}, {id: 2, name: "Kiwis"}] 

I.e. we iterate through all items in the array, and if the item has a certain ID, we change the name property to Bananas, otherwise we just keep the item as-is.

1 Like

in this example is the id the index or is the id an explicitly declared object element?

I am currently having the issue that I have list of things repeated with values from an array - that works well - but I want to be able to tap on an element in that list and have it change a value in the array. For some reason, “Currently Repeated property value” is grey’d out when I try and select it as a variable name in the Set page Variable logic.

In the example, ID is an explicit property that just happens to be the same as the item index.

The way you’re thinking is correct, but you can’t currently target a specific item in an array directly. Instead, you need to change the whole array to a new one, using MAP to change it – so the same formula as above, but item.id === current.id.

Brill - that replacement iteration works - thanks so much.

But if I know the index of the object that I want to change, can I address it directly or do I still have to iterate though the array that contains it?

And furthermore - what if I want to change the value of two objects? What is the formula syntax for more than one SET_KEY instruction in an IF statement?

Yes you can access any index of an array by myArray[0] notation, 0 being the first element. Or you can use the LOOKUP function.

Don’t think there’s a specific function for setting multiple keys, you could always request it in the feature requests tracker.appgyver.com - in the meantime I’m sure you can find a way to do it using MAP or REDUCE

1 Like

So something like:

IF(item.id === "first_id", SET_KEY(...), IF(item.id === "second_id", SET_KEY(...), item))

Cheers - here’s another iteration question.

I have one array of strings and another array of objects. I want to remove the objects in the second string if their parameter “Country” exists in the first array. I’m trying:

MAP(arraypageVars.testSelected, REMOVE_ITEM_BY_KEY(pageVars.DropDownArray, “Country”, item))

Where 'testSelected" is an array of 10 strings that either have a country name or “No Country” and DropDownArray contains 18 objects {“Country” : country name}.

For context - I’m trying to remove a country from a list once it’s been selected in a dropdown.

You would then use the SELECT function

SELECT(pageVars.DropDownArray, IS_IN_ARRAY(pageVars.testSelected, item.Country))

This does exactly the OPPOSITE of what I want!!!

Select (as written above) returns DropDownArray with ONLY the country in testSelected and everything else removed.

What I want to do is to remove the testSelected country/ies from DropDownArray, leaving everything else intact.