Created
April 9, 2015 16:59
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* [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