How to find and return a value in a simple array

I’m looking to extract an item from a simple array received as a value from an API call.

Basically I have an array: [“banana”, “specialprefix-orange”, “apple”]. I’m looking to return the item with the “specialprefix-”, in this case “orange”. This is simple enough to do with a loop in Javascript, but it doesn’t seem so simple in Make.

Any suggestions on how to do this either with modules or functions?

1 Like

Hi @Benjamin_Long
You can iterate through the array to check if the the value contains “specialprefix-” in it:

Iterate through the array:


Filter the value contains “specialprefix-” in it

Output:

If you want only the orange without the prefix then you can replace the prefix with empty string:


Output:

Best regards,

Msquare Automation
Gold Partner of Make
@Msquare_Automation

Welcome to the Make community!

Yes, that is possible. You’ll need a minimum of one module (one operation).

Screenshot

Output

Screenshot_2024-08-20_120853

Module Export - quick import into your scenario

You can copy and paste this module export into your scenario. This will import the modules (with fields/settings/filters) shown in my screenshots above.

  1. Move your mouse over the line of code below. Copy the JSON by clicking the copy button on the right of the code, which looks like this:

  2. Enter your scenario editor. Press ESC to close any dialogs. Press CTRLV (paste keyboard shortcut for Windows) to paste directly in the editor.

  3. Click on each imported module and re-save it for validation. There may be some errors prompting you to remap some variables and connections.

JSON module export — paste this directly in your scenario

{"subflows":[{"flow":[{"id":189,"module":"regexp:Parser","version":1,"parameters":{"pattern":"\\b(?<item>specialprefix-.+?)\\b","global":false,"sensitive":true,"multiline":false,"singleline":false,"continueWhenNoRes":false},"mapper":{"text":"{{join(187.array; space)}}"},"metadata":{"designer":{"x":2760,"y":352},"parameters":[{"name":"pattern","type":"text","label":"Pattern","required":true},{"name":"global","type":"boolean","label":"Global match","required":true},{"name":"sensitive","type":"boolean","label":"Case sensitive","required":true},{"name":"multiline","type":"boolean","label":"Multiline","required":true},{"name":"singleline","type":"boolean","label":"Singleline","required":true},{"name":"continueWhenNoRes","type":"boolean","label":"Continue the execution of the route even if the module finds no matches","required":true}]}}]}],"metadata":{"version":1}}

Note: Did you know you can reduce the size of blueprints and module export code like the above, using the Make Blueprint Scrubber?

Hope this helps! Let me know if there are any further questions or issues.

@samliew


P.S.: Did you know, the concepts of about 70% of questions asked on this forum are already covered in the Make Academy. Investing some effort into it will save you lots of time and frustration using Make later!

2 Likes

Thanks for the rapid replies!

I ended up going with the regex option because it would be a single module and because for some reason the iterator was not providing a “Value” field for me to select, even after running data through it.

1 Like