Skip to content

Instantly share code, notes, and snippets.

@ubergoober
Last active February 3, 2016 17:47
Show Gist options
  • Save ubergoober/d0ece002063088d898de to your computer and use it in GitHub Desktop.
Save ubergoober/d0ece002063088d898de to your computer and use it in GitHub Desktop.
Check if value for a key exists in an array of objects.
var arrayOfObjects = [{key1: 'foo1', key2: 'bar1'}, {key1: 'foo2', key2: 'bar2'}];
var keyNameToCheck = "key1";
var valueToFind = 'foo1';
function doesArrayOfObjectsContainValueForKey(array, keyName, value){
var res = array.filter(function(obj){
return obj[keyName] === value;
});
return res.length === 0 ? false : true;
}
//usage
doesArrayOfObjectsContainValueForKey(arrayOfObjects, keyNameToCheck, valueToFind);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment