Skip to content

Instantly share code, notes, and snippets.

@afaulkinberry
Last active August 29, 2015 14:26
Show Gist options
  • Save afaulkinberry/f51909563c6a6390184c to your computer and use it in GitHub Desktop.
Save afaulkinberry/f51909563c6a6390184c to your computer and use it in GitHub Desktop.
AngularJS - New array from object array based on property value
$scope.sortedArr = function(prop, propVal, masterArr){
var newArr = [];
angular.forEach(masterArr, function(value, key) {
if(angular.equals(value[prop], propVal)){ newArr.push(value); }
});
return newArr;
};
var objArr = [
{ "first_name" : "George", "last_name" : "Washington" },
{ "first_name" : "John", "last_name" : "Adams" },
{ "first_name" : "George", "last_name" : "Harrison" },
{ "first_name" : "John", "last_name" : "Mellancamp" },
{ "first_name" : "George", "last_name" : "Jetson" },
{ "first_name" : "John", "last_name" : "Bravo" }
];
var fNameArr = $scope.sortedArr('first_name', 'George', objArr);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment