Concatenation of multiple selections (checkboxes) while filtering duplicates

Greetings-

I am attempting to create an equation that will take several fields that use checkboxes and combine the selections into one text field. These different fields have duplicate options for checkboxes, so I’d like to only show one instance of the duplicated selection.

NOTE: for my use, it is not necessary to identify which field the selection came from, I just want to combine them all into one text field with each option separated with a comma.

@Chem, you are the wizard…any thoughts?

Cheers,
Adam

Here’s how you can do this using the Custom Javascript pipe.

Click here to watch a video on how to set this up.

You can find the pipe request code below.

Array.prototype.contains = function(v) {
  for (var i = 0; i < this.length; i++) {
    if (this[i] === v) return true;
  }
  return false;
};

Array.prototype.unique = function() {
  var arr = [];
  for (var i = 0; i < this.length; i++) {
    if (!arr.contains(this[i])) {
      arr.push(this[i]);
    }
  }
  return arr;
}

var concatAll = "{value1}" + ',' + "{value2}"  + ',' + "{value3}"  + ',' + "{value4}"  + ',' + "{value5}";
var duplicatesArray = concatAll.split(',');

var uniques = duplicatesArray.unique();
uniques.toString().replace(/,/g, ", ");
2 Likes

@Chem

This is fantastic! Thank you so much! Not to be greedy but can you advise if adding a space after each comma is feasible…also my array seems to be cut off (see picture).

Any thoughts?
Adam

Hey Adam, for the space after each comma, you can update the last line in the code to the following.

uniques.toString().replace(/,/g, ", ");

I’ve updated the post above to reflect this.

All, just note…when you establish the Unique Array text field, make sure you select “Long Text” vs “Text” field. Simple fix for me.

1 Like