Cheat sheet - JS code snippets

Checking duplicates in an array

// Method 1
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
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 duplicates in a JSON object


Checking whether an array contains an element


Extracting data from a timestamp


Getting today's date


Extracting file names from the UploadFile component


Getting a specific attribute value of a component


Removing spaces


Matching a value against a mask (regex)


Counting occurrences of a given value


Checking how many full years have passed


Retrieving data from a repeatable section


Calling ServiceProxy


Creating a dictionary


Formatting floating point numbers


Returning the day/month/year suffix


Creating an array from a list of objects


Filtering an array


Removing selected properties from an object

Removing duplicates from an array using Set


Getting values using inputParameters


Creating a BigDecimal and useful helpers


Formatting account balance

Last updated

Was this helpful?