Cheat sheet - JS code snippets
The code snippets below present example ways to implement commonly used operations in ScriptCode. These are simplified patterns that you should adapt yourself to the specific use case, including parameter names, artifact type, expected output format, and required business logic.
Checking for duplicates in an array
Method 1
Used to clean data. Iterates through the array and returns a new one with duplicates removed.
function distinct(array) {
const a = [];
for (let i = 0, l = array.length; i < l; i++) {
if (a.indexOf(array[i]) === -1) a.push(array[i]);
}
return a;
}Method 2
Intended for validation only. Returns only the boolean value true or false. Asks: "Are there any duplicates in this list?". Does not return cleaned data.
function hasDuplicates(array) {
const valuesSoFar = Object.create(null);
for (let i = 0; i < array.length; i++) {
const value = array[i];
if (value in valuesSoFar) return true;
valuesSoFar[value] = true;
}
return false;
}Removing empty values from an array
Checking for duplicates in a JSON object
Checking whether an array contains an element
Method 1
Method 2
Extracting data from a timestamp
Getting today's date
Extracting file names from the UploadFile component
Getting the value of a specific component attribute
Removing spaces
Matching a value to a mask (regex)
Counting occurrences of a given value
Method 1
Method 2
Checking how many full years have passed
Getting data from a repeatable section
ServiceProxy call
Creating a dictionary
Method 1
Method 2
Formatting floating-point numbers
Returning the day/month/year suffix
Creating an array from a list of objects
Filtering an array
Removing selected object parameters
Method 1
Method 2
Removing duplicates from an array using Set
Getting values using inputParameters
Creating a BigDecimal and useful add-ons
Formatting account balance
Creating and throwing a business error
Using the spread operator ...
Method 1
Example of extending customer data.
Output:
Method 2
Example of combining two lists into one.
Handling default parameters Default Parameters
Output:
Last updated
Was this helpful?
