Skip to content

Instantly share code, notes, and snippets.

@afaulkinberry
Last active March 5, 2019 21:41
Show Gist options
  • Save afaulkinberry/45c04880ab5dbdd85222 to your computer and use it in GitHub Desktop.
Save afaulkinberry/45c04880ab5dbdd85222 to your computer and use it in GitHub Desktop.
AngularJS - Remove duplicate objects from object array
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" }
];
@avkcorporation
Copy link

thanks too 👍

@gemechisak
Copy link

Thank you!

@FrozenCaptain
Copy link

Good work! Small and Easy to use. Thank you!

@ShahzaibSaleem154
Copy link

Very useful solution. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment