Last active
March 5, 2019 21:41
-
-
Save afaulkinberry/45c04880ab5dbdd85222 to your computer and use it in GitHub Desktop.
AngularJS - Remove duplicate objects from object array
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
function(arr){ | |
var newArr = []; | |
angular.forEach(arr, function(value, key) { | |
var exists = false; | |
angular.forEach(newArr, function(val2, key) { | |
if(angular.equals(value.id, val2.id)){ exists = true }; | |
}); | |
if(exists == false && value.id != "") { newArr.push(value); } | |
}); | |
return newArr; | |
} | |
var objArr = [ | |
{ "id" : "1", "first_name" : "George", "last_name" : "Washington" }, | |
{ "id" : "2", "first_name" : "John", "last_name" : "Adams" }, | |
{ "id" : "3", "first_name" : "George", "last_name" : "Harrison" }, | |
{ "id" : "4", "first_name" : "John", "last_name" : "Mellancamp" }, | |
{ "id" : "5", "first_name" : "George", "last_name" : "Jetson" }, | |
{ "id" : "3", "first_name" : "John", "last_name" : "Bravo" } | |
]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Very useful solution. Thank you!