How to update a specific value in an array?

Hi guys and thanks for your answers, they have already helped me a lot.

I just want to do like in javascript : MyArray[4]=‘new value’

Thank you
blueprint (1).json (4.8 KB)

In the blueprint, I just want to update the second value of my array Arr.

Welcome to the Make community!

You cannot update an existing value in an array variable. By using “Set Variable” module, you are essentially creating a new array variable.

There are more than one way to create a new array with the updated value at the index you specify.

1. Merge, and use regex to replace the index, then split back

Output
Screenshot_2024-10-10_111012

2. Use an Iterator and Text Aggregator with a “if” function, then split back

Screenshot_2024-10-10_111032

Output

3. Use JavaScript in a CustomJS module

Output
Screenshot_2024-10-10_111012 (2)

4. Use AI to replace the value at the array’s index

This method may be prone to errors if the AI is dumb or your prompt is poorly constructed.

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":10,"module":"util:SetVariable2","version":1,"parameters":{},"mapper":{"name":"array","scope":"roundtrip","value":"{{split(\"a1,a2,a3\"; \",\")}}"},"metadata":{"designer":{"x":334,"y":515,"name":"Method 3","messages":[{"category":"link","severity":"warning","message":"The module is not connected to the data flow."}]}}},{"id":11,"module":"custom-js:inlineexecutev2","version":1,"parameters":{"__IMTCONN__":2457341,"jscode":"const arr = input.array;\narr[input.replace_index] = input.replace_value;\nreturn arr;","returnValueType":"array"},"mapper":{"input":"{\n  \"array\": [\"{{join(10.array; \"\"\",\"\"\")}}\"],\n  \"replace_index\": 1,\n  \"replace_value\": \"b2\"\n}"},"metadata":{"designer":{"x":577,"y":514},"parameters":[{"name":"__IMTCONN__","type":"account:custom-js2","label":"Connection","required":true},{"name":"jscode","type":"text","label":"JavaScript Code","required":true},{"name":"returnValueType","type":"select","label":"Return Type","required":true,"validate":{"enum":["text","collection","array","binary"]}}]}}]},{"flow":[{"id":6,"module":"util:SetVariable2","version":1,"parameters":{},"mapper":{"name":"array","scope":"roundtrip","value":"{{split(\"a1,a2,a3\"; \",\")}}"},"metadata":{"designer":{"x":332,"y":218,"name":"Method 2","messages":[{"category":"link","severity":"warning","message":"The module is not connected to the data flow."}]}}},{"id":7,"module":"builtin:BasicFeeder","version":1,"parameters":{},"mapper":{"array":"{{6.array}}"},"metadata":{"designer":{"x":573,"y":220}}},{"id":8,"module":"util:TextAggregator","version":1,"parameters":{"rowSeparator":"other","otherRowSeparator":"|","feeder":7},"mapper":{"value":"{{if(7.`__IMTINDEX__` = 2; \"b2\"; 7.value)}}"},"metadata":{"designer":{"x":815,"y":220},"parameters":[{"name":"rowSeparator","type":"select","label":"Row separator","validate":{"enum":["\n","\t","other"]}},{"name":"otherRowSeparator","type":"text","label":"Separator"}],"advanced":true}},{"id":9,"module":"util:SetVariable2","version":1,"parameters":{},"mapper":{"name":"new_array","scope":"roundtrip","value":"{{split(8.text; \"|\")}}"},"metadata":{"designer":{"x":1061,"y":219,"name":"New Array"}}}]},{"flow":[{"id":3,"module":"util:SetVariable2","version":1,"parameters":{},"mapper":{"name":"array","scope":"roundtrip","value":"{{split(\"a1,a2,a3\"; \",\")}}"},"metadata":{"designer":{"x":326,"y":-66,"name":"Method 1"}}},{"id":4,"module":"util:SetVariable2","version":1,"parameters":{},"mapper":{"name":"new_array","scope":"roundtrip","value":"{{split(replace(join(3.Arr; \"|\"); \"/(?<=(?:[^|]+\\|){1})[^|]+/\"; \"b2\"); \"|\")}}"},"metadata":{"designer":{"x":571,"y":-65,"name":"New Array"}}}]}],"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.: Investing some effort into the Make Academy will save you lots of time and frustration using Make.

3 Likes

How to set-up a FREE Custom JS connection?

  1. Go to the app’s website and click “Create Account”.

  2. Once logged in, on the right-hand side, click on the “Show” link. Copy the API key.

  3. Click “Create a connection” in the Custom JS module:

  4. Paste API key and click Save:

How to use the Custom JS module?

  1. Map a variable (or a JSON object) into the “Input” field.

  2. Write a function to do something with the input variable. If the input is a JSON, you can reference object properties using dot notation.

  3. Write a final statement to return the call of the function you wrote.

Further Information

If you need help with using Custom JS modules, please create a new thread. This helps keep the forum organized and helps me get to your question faster.

You can also take a look at the official documentation here for more advanced usage:

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

@samliew

P.S.: Investing some effort into the Make Academy will save you lots of time and frustration using Make.

3 Likes

Thank you very much !