Noob question about Javascript

Hi everyone! :blush:

I’m trying to get some help with a piece of JavaScript code I’m using in Make. The code below works, but it only returns a single output with all the values on the same line. I’d like to have multiple usable variables (one output for formattedPrenom, another for formattedNom, and a third one for utm_variable).

Here’s the code I’m using:

javascript

function removeAccents(str) {
    return str.normalize('NFD').replace(/[\u0300-\u036f]/g, '');
}

function formatNamePart(namePart) {
    return namePart
        .replace(/[^a-zA-ZÀ-ÿ\s-]/g, '')
        .trim()
        .split(' ')
        .map(word => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase())
        .join(' ');
}

function formatForUrl(namePart) {
    return removeAccents(namePart)
        .toLowerCase()
        .replace(/\s+/g, '-');
}

let first_name = inputData.first_name;
let last_name = inputData.last_name;

let formattedPrenom = formatNamePart(first_name);
let formattedNom = formatNamePart(last_name);

let utm_variable = `${formatForUrl(formattedPrenom)}.${formatForUrl(formattedNom)}`;

// Retourner les variables comme une liste de valeurs
return [
    formattedPrenom,
    formattedNom,
    utm_variable
];

However, I’m only getting one output with all the values together on the same line. What I’d like is to have separate outputs so I can use formattedPrenom, formattedNom, and utm_variable as individual variables in my workflow.

Can you help me?
Thanks a lot for your help! :blush:

You are close, wrap the free parts with a function, and call the function with “input” variable.

Then, select Return Type “Object” or “Array”.

Example input/output

Screenshot_2024-08-29_210847

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":328,"module":"custom-js:inlineexecutev2","version":1,"parameters":{"__IMTCONN__":2457341,"jscode":"function removeAccents(str) {\n  return str.normalize('NFD').replace(/[\\u0300-\\u036f]/g, '');\n}\nfunction formatNamePart(namePart) {\n  return namePart.replace(/[^a-zA-ZÀ-ÿ\\s-]/g, '').trim().split(' ').map(word => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase()).join(' ');\n}\nfunction formatForUrl(namePart) {\n  return removeAccents(namePart).toLowerCase().replace(/\\s+/g, '-');\n}\nfunction main(inputData) {\n  let first_name = inputData.first_name;\n  let last_name = inputData.last_name;\n  let formattedPrenom = formatNamePart(first_name);\n  let formattedNom = formatNamePart(last_name);\n  let utm_variable = `${formatForUrl(formattedPrenom)}.${formatForUrl(formattedNom)}`;\n  // Retourner les variables comme une liste de valeurs\n  return [\n    formattedPrenom,\n    formattedNom,\n    utm_variable\n  ];\n}\nreturn main(input);","returnValueType":"array"},"mapper":{"input":"{\n  \"first_name\": \"{{329.first_name}}\",\n  \"last_name\": \"{{329.last_name}}\"\n}"},"metadata":{"designer":{"x":373,"y":-2984},"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"]}}]}}]}],"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?
e

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!

3 Likes

If you want to map the variables easier, I suggest using an object instead.

To do this, change the array square brackets [ ] around the return statement into curly brackets like this { }

e.g.:

Screenshot_2024-08-29_220835

Example Output

Screenshot_2024-08-29_220859

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!