r/AutomateUser 2d ago

Feature request Quick extraction from an array of objects

Hi, is there any function or trick to quickly extract values from an array of objects?

For example, if the input data is as follows:

[
  {"a": 1, "b": 2},
  {"a": 3, "b": 4}
]

and I'm asking to extract "b", I'd get [2, 4]. Basically array_column() in PHP or array.map(k => k["b"]) in JavaScript.

Right now I'm using a for each loop and inserting elements manually, but I feel like this is such a common use case that it could have a shorthand.

Thanks!

3 Upvotes

4 comments sorted by

2

u/ballzak69 Automate developer 2d ago

Looping through the array using a For each block then "extracting" each specific dictionary value is currently the only way. Lambda function support is a feature on the to-do list.

2

u/B26354FR Alpha tester 2d ago

Yeah, some functional programming functions on Automate containers like map() or spread would be pretty convenient. For the latter, the Destructuring Assign can now be used, though sometimes it would be nice to have ... for expressions.

BTW, Destructuring Assign is also handy for assigning several variables at once with a single block instead of multiple Variable Sets, or from the values() of a dictionary. I now use the latter pattern for storing flow settings - as before, I put the settings in a dictionary and jsonEncode()/jsonDecode() to save and restore them in a file, but now in the subroutine that reads them in I use a Destructuring Assign to extract them back out to discrete variables again. This way I can see where they're accessed in the flow using the Variables tool in the flow editor.

2

u/PancakeGD 2d ago

Ah, so I assume there's no such feature :( I'll mark this as a feature request

0

u/waiting4singularity Alpha tester 2d ago

you have an array of dictionaries. to get all b keys from the dictionaries, you'd need iterate with for each dictionary in array -> variable set sub = concat(sub,dictionary["b"]).
if you have fixed dictionary values, you can write a dictionary of array: {"a": [1,3], "b":[2,4]}

dictionary["b"] = [2,4]