Skip to content

Instantly share code, notes, and snippets.

@loren138
Last active July 6, 2016 16:53
Show Gist options
  • Save loren138/521c61a2edd84d38836294ca1b1f34fb to your computer and use it in GitHub Desktop.
Save loren138/521c61a2edd84d38836294ca1b1f34fb to your computer and use it in GitHub Desktop.
Angular Grep (find value using array of objects with id or other property)

Angular Grep

Find value using array of objects with id or other property.

Usage

In the controller:

$scope.myArrayofTypes = [
    { id: 1, name: 'Test' },
    { id: 2, name: 'Hey' }
];
$scope.myArray = [2, 1];

In the view:

<div>
  <ul>
    <li ng-repeat='typeId in myArray'>
        {{myArrayofTypes | objectKeyValueShow:'id':typeId:'name'}}
    </li>
  </ul>
</div>
(function() {
'use strict';
angular.module('something')
.filter('objectKeyValueShow', function () {
return function (input, key, value, show) {
if (jQuery.isArray(input)) {
for (var item in input) {
if (input.hasOwnProperty(item) && input[item][key] === value) {
return input[item][show];
}
}
}
return null;
}
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment