Skip to content

Instantly share code, notes, and snippets.

@ryanhamley
Created April 9, 2015 16:59
Show Gist options
  • Save ryanhamley/fbe231d9f61c795d4a45 to your computer and use it in GitHub Desktop.
Save ryanhamley/fbe231d9f61c795d4a45 to your computer and use it in GitHub Desktop.
Recursive function for Angular.js to determine if an array of objects contains duplicates
/**
* [checkForDuplicateObjects recursively checks an array of objects for duplicate values using angular.equals() to deeply compare objects.]
* @param {[array]} _array [the array of objects to be checked]
* @return {[boolean]} true or falsey [the function will return true if duplicates are found]
*/
function checkForDuplicateObjects(_array) {
if (_array.length > 1) {
var first = _array[0];
for (var i = 1; i < _array.length; i++) {
if (angular.equals(first, _array[i])) {
return true;
}
}
_array.shift();
return checkForDuplicateObjects(_array);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment