I would like to get a distinct array of objects. The code below is the first thing that came to mind.
Do you see any room for improvements?
function distinct(objectArray){
var distinctResult = [];
$.each(objectArray, function(i, currentObject){
if(!exists(distinctResult, currentObject))
distinctResult.push(currentObject);
});
return distinctResult;
}
function exists(arr, object){
var compareToJson = JSON.stringify(object),
result = false;
$.each(arr, function(i, existingObject){
if(JSON.stringify(existingObject) === compareToJson) {
result = true;
return false; // break
}
});
return result;
}
var testList = [{ foo: 1 }, { foo: 1 }, { bar: 1 }, { bar: 2, baz: 3 }];
console.log(distinct(testList));