Expand my Community achievements bar.

Latest Community Ideas Review is Out: Discover What’s New and What to Expect!
SOLVED

How can I transform a list of items (or a collection) in a single bundle to an array that can be iterated through?

Avatar

Community Advisor

I have a working solution that I am looking to simplify or make more efficient.

There are times that a module will return a single record (Project) either through a Search Record module or a Custom API call module. In this example, I'm using both: a Custom API call so I can include parameterValues in the fields list; followed by a Search Record (for the same project) where I map the parameterValues collection that was returned in the response of the API call to the Outputs field of the Search Record module. This creates a dynamic way to pull in all of a project's custom data in the Search Record module without having to hard code the output selections.

In this example, the record that is returned is a single bundle, and that bundle is a list of items (name, ID, DE:this, DE:that). What I need to do is iterate through that list of items, not knowing in advance which DE:fields may or may not be in the list. (I'm using contains on the name to determine if actions should or shouldn't happen, and the value to determine what actions should happen).

I can get a simple array of the DE:fields by using {{keys(**.body.data.parameterValues)}} as an input to a Set Variable module - but this only returns an array of the item names, without their corresponding values.

I then set an Iterator to work through that Set Variable module with an input of {{if(contains(**.value; "DE:QTY"); **.value; "")}} (In this example, if the name of the custom field being evaluated begins with QTY, get the value of the custom field.)

I've managed to make this work, but it feels like I should be able to map both the names AND the values to an array of collections of key:value pairs.

Is there some combination of array functions (get, map, keys?) that I can use in a Set Variable module (or as in input to an iterator module) that will yield an array of all the names and values from a single bundle of items, or is there a way to break that single bundle of items into multiple bundles of key:value pairs?

So from this:

parameterValues (collection)

DE:customField1 : custom value 1

DE:customField2 : custom value 2

DE:customField3 : custom value 3

to this:

parameterValues [array]

1 (collection)

name: DE:customField 1

value: custom value 1

2 (collection)

name: DE:customField 2

value: custom value 2

3 (collection)

name: DE:customField 3

value: custom value 3

TLDR: I want to iterate through items in a single bundle (using both key and value).

If you like my content, please take a moment to view and vote on my Idea Requests: https://tinyurl.com/4rbpr7hf
Topics

Topics help categorize Community content and increase your ability to discover relevant content.

1 Accepted Solution

Avatar

Correct answer by
Level 4

I suppose we may want to consider a "convert collection to Array" function at some point.

In the meantime, this scenario blueprint will do the trick. (The tip is to iterate with keys function as you have done..then get the value of the key in each iteration using get). See attached blueprint -- which does not include my real age :)

View solution in original post

4 Replies

Avatar

Community Advisor

That is a handy trick, Doug; but not exactly what I'm after here. I was looking to convert the collection of parameterValues in an http response to an array so it could be processed more efficiently.

If you like my content, please take a moment to view and vote on my Idea Requests: https://tinyurl.com/4rbpr7hf

Avatar

Correct answer by
Level 4

I suppose we may want to consider a "convert collection to Array" function at some point.

In the meantime, this scenario blueprint will do the trick. (The tip is to iterate with keys function as you have done..then get the value of the key in each iteration using get). See attached blueprint -- which does not include my real age :)

Avatar

Community Advisor

Thanks Darin. I'm definitely interested in a convert Collection to Array function. This can simplify scenarios that need to perform logic that is conditionally driven by the name of list items; and subsequently their value.

Glad to know my workaround is as good as it gets and I wasn't overlooking the obvious answer, like I might have done when I was 21...

If you like my content, please take a moment to view and vote on my Idea Requests: https://tinyurl.com/4rbpr7hf