Design & Download
Your JS Library
in Seconds!!!

Snippet: uniquify()

Written by Christopher West (cwest) on November 14, 2015.
Creates a new version of an array in which each value is unique.
function uniquify(anArray) {
  anArray = slice(anArray);
  for (var e1, i1 = 0, l = anArray.length; i1 < l; i1++) {
    e1 = anArray[i1];
    for (i2 = i1 + 1; i2 < l; i2++) {
      if (e1 === anArray[i2]) {
        l--;
        anArray.splice(i2--, 1);
      }
    }
  }
  return anArray;
}

If you want to get only the unique elements in an array you can use YourJS.uniquify(...).

uniquify(...) API Documentation

Parameters

  1. anArray {Array}:
    The array which will be duplicated and returned without any duplicate values.

Returns

Returns a copy of anArray with each value only appearing one time.