jList: Two new functions to dedupe lists in Javascript

Earlier today I released version 1.1.0 of the jList Javascript library. This release adds two new functions, bringing the total to 15.

The new functions are:

  • listRemoveDuplicates
  • listRemoveDuplicatesNoCase

As you might guess from the names these are intended to remove duplicate elements from lists.

A simple code snippet illustrates the difference between the two:

mylist = 'cat,dog,monkey,DOG,Dog,cat';

response = jList.listRemoveDuplicates(mylist);
console.log (response);

response = jList.listRemoveDuplicatesNoCase(mylist);
console.log (response);

In line 3 we perform case-sensitive matching when looking for duplicates. As a result, we don’t consider ‘dog’ to be the equivalent of ‘DOG’ or ‘Dog’ so the result displayed in line 4 will be ‘cat,dog,monkey,DOG,Dog’ – only the duplicate ‘cat’ has been removed.

In line 4 we perform case-insensitive matching when looking for duplicates. As a result, the output from line 7 will be ‘cat,dog,monkey’ as ‘dog’, ‘DOG’ and ‘dog’ are all considered equivalent in this case.

Notice also that in both functions the order of the list elements is unchanged. This illustrates that it’s the second and subsequent occurrences of an element that are considered to be the duplicates and removed, the first occurrence remains in its original place within the list.

The new version of jList, including updated documentation, is available for download from GitHub.



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s