Distinct Records in Object Array

(Last Updated On: )

Sometimes you need to determine the distinct objects in an array or distinct values in array. There are so many ways to do this. One way which I have used at times can be a bit slow depending on the size of your array.
From my investigation there is a lodash version that is much better. Once I do some testing I will update this but for now here is an example.
I expanded on the idea from Stack Exchange.

 var distinct = function(objectArray, param){
      var distinctResult = [];

      $.each(objectArray, function(i, currentObject){
            if (param !== null) {
                  if (distinctResult.filter(function(v) { return v[param] == currentObject[param]; }).length == 0)
                  {
                        distinctResult.push(currentObject);
                  }
            } else {
                  if(!exists(distinctResult, currentObject))
            {
                  distinctResult.push(currentObject);
            }
            }
      });

      return distinctResult;
};

var exists = function(arr, object){
    var compareToJson = JSON.stringify(object);
    var result = false;
    $.each(arr, function(i, existingObject){
        if(JSON.stringify(existingObject) === compareToJson) {
            result = true;
            return false; // break
        }
    });

    return result;
};